这个神经网络计算从摄氏度到多少法门度
====================================================== ===============
`import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.python.keras.layers import Dense
c = np.array([-40, -10, 0, 8, 15, 22, 38])
f = np.array([-40, 14, 32, 46, 59, 72, 100])
model = keras.Sequential()
model.add(Dense(units=1, input_shape=(1,), activation='linear'))
model.compile(loss='mean_squared_error', optimizer=keras.optimizers.Adam(0.1))
history = model.fit(c, f, epochs=500)
plt.plot(history.history['loss'])
plt.grid(True)
`plt.show()
====================================================== ================
错误在这一行:
history = model.fit(c, f, epochs=500)
`here is the part of my code where the terminal displayed the error:
Traceback (most recent call last):
File "d:\Programming\Python\AI\With_tensorflow\One\Neyros\NeyroOptimization.py", line 25, in <module>
model.add(Dense(units=1, input=(1,), activation='linear'))`
这样代码就不会出错并且可以工作
您的错误来自您的导入,
from tensorflow.keras.layers import Dense #<- Correct import
在这里试试这个代码
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
c = np.array([-40, -10, 0, 8, 15, 22, 38])
f = np.array([-40, 14, 32, 46, 59, 72, 100])
model = keras.Sequential()
model.add(Dense(units=1, input_shape=(1,), activation='linear')) # Correct argument
model.compile(loss='mean_squared_error', optimizer=keras.optimizers.Adam(0.1))
# Train the model
history = model.fit(c, f, epochs=500)
# Plot the loss
plt.plot(history.history['loss'])
plt.grid(True)
plt.show()