Tensorflow中的numpy问题

bvjveswy  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(219)
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()`

这是我的代码,我需要帮助。
我希望正常的事情,一些图表显示,它说,一切都工作finde.但它没有.错误代码:

Traceback (most recent call last):
  File "C:\Users\name\PycharmProjects\Neural network\first_self_approach.py", line 21, in <module>
    model.fit(x_train, y_train, epochs=10)

  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None

  File "C:\Users\name\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\data_adapter.py", line 1859, in _get_tensor_types
    return (tf.Tensor, np.ndarray, pd.Series, pd.DataFrame)

AttributeError: module 'pandas' has no attribute 'Series'
sg24os4d

sg24os4d1#

Meh,这里有很多错误。
1.这不是一个分类问题,这是一个回归问题。
1.加载数据的顺序是错误的。
1.你怎么能用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()

还有很多事情你可以做,以改善这个模型

相关问题