[英]Solve InvalidArgumentError in tensorflow
我也沒有什么可以說的,因為我收到了一個我似乎無法從下面的代碼中解決的錯誤。
import tensorflow as tf
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
inpTensor = tf.keras.layers.Input(x_train.shape[1:],)
hidden1Out = tf.keras.layers.Dense(units=128, activation=tf.nn.relu)(inpTensor)
hidden2Out = tf.keras.layers.Dense(units=128, activation=tf.nn.relu)(hidden1Out)
finalOut = tf.keras.layers.Dense(units=10, activation=tf.nn.softmax)(hidden2Out)
model = tf.keras.Model(inputs=inpTensor, outputs=finalOut)
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train,y_train, epochs = 4)
我嘗試將損失 function 更改為“categorical_crossentropy”,但似乎也不起作用。 我正在運行 Python 3.7 並且真的會提供一些幫助。 我對此也很陌生。
提前致謝。
問題在於您管理網絡維度的方式......您收到 3D 圖像並且不傳遞到 2D 以獲得概率......這可以簡單地使用 Flatten 或全局池操作來完成。 sparse_categorical_crossentropy 在您的情況下是正確的。 這是一個例子
import tensorflow as tf
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
inpTensor = tf.keras.layers.Input(x_train.shape[1:],)
hidden1Out = tf.keras.layers.Dense(units=128, activation=tf.nn.relu)(inpTensor)
hidden2Out = tf.keras.layers.Dense(units=128, activation=tf.nn.relu)(hidden1Out)
pooling = tf.keras.layers.GlobalMaxPool2D()(hidden2Out) #<== also GlobalAvgPool2D or Flatten are ok
finalOut = tf.keras.layers.Dense(units=10, activation=tf.nn.softmax)(pooling)
model = tf.keras.Model(inputs=inpTensor, outputs=finalOut)
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train,y_train, epochs = 4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.