numpy在Tensorflow中的问题


from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
data = keras.datasets.boston_housing
(x_train, x_test), (y_train, y_test) = data.load_data()
model = keras.Sequential([
keras.layers.InputLayer(13),
keras.layers.Dense(3, activation="relu"),
keras.layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics="accuracy")
model.fit(x_train, y_train, epochs=10)
predict = model.predict(x_test)
for i in range(10):
plt.grid(False)
plt.imshow(x_test[i], cmap=plt.cm.binary)
plt.suptitle("Actual: " + y_test[i])
plt.title("Prediction: " + np.argmax(predict[i]))
plt.show()`

这是我的代码,我需要帮助

我期待正常的事情,一些图表显示,它说,一切都已找到。但事实并非如此。错误代码:

Traceback (most recent call last):
File "C:UsersnamePycharmProjectsNeural networkfirst_self_approach.py", line 21, in <module>
model.fit(x_train, y_train, epochs=10)
File "C:UsersnameAppDataLocalProgramsPythonPython310libsite-packageskerasutilstraceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UsersnameAppDataLocalProgramsPythonPython310libsite-packageskerasenginedata_adapter.py", line 1859, in _get_tensor_types
return (tf.Tensor, np.ndarray, pd.Series, pd.DataFrame)
AttributeError: module 'pandas' has no attribute 'Series'

嗯,这里有很多错误。

  1. 这不是一个分类问题,这是一个回归问题。
  2. 数据加载顺序错误
  3. 如何使用imshow绘制数据?你连图片都没有

这里我给你一个工作的例子:

from tensorflow import keras 
import matplotlib.pyplot as plt 
import numpy as np
data = keras.datasets.boston_housing
(x_train, y_train), (x_test, y_test) = data.load_data()
model = keras.Sequential([
keras.layers.Dense(3, activation="relu", input_shape=(13,)),
keras.layers.Dense(1)
])
model.compile(optimizer="adam", loss="mse")
model.summary()
model.fit(x_train, y_train, epochs=20)
predict = model.predict(x_test)
plt.scatter(y_test, predict)
plt.show()

你还可以做很多事情来改进这个模型

相关内容

  • 没有找到相关文章

最新更新