[英]Expected shape of Keras Dense layer output with 300 units is (1,)
我想擁有簡單的單層NN,它將NN的矢量轉換為300的矢量。
因此具有:
print(np.array(train_in).shape)
print(np.array(train_t).shape)
返回:
(943, 300)
(943, 300)
我嘗試以下方法:
model = keras.Sequential()
model.add(Dense(300, input_shape=(300,)))
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(np.array(train_in), np.array(train_t), epochs=5)
我越來越:
ValueError: Error when checking target: expected dense_37 to have shape (1,) but got array with shape (300,)
為什么期望目標具有形狀(1,)
? 具有300個單位的圖層應在輸出上產生300個數字的向量,對嗎?
編輯:
根據要求,這是我的數據的樣子:
print(np.array(train_in))
print(np.array(train_t))
給出:
[[-0.13841234 0.22157902 0.12244826 ... -0.10154381 -0.01824803
-0.08607237]
[ 0.02228635 0.3353927 0.05389142 ... -0.23218463 -0.06550601
0.03365546]
[ 0.22719774 0.25478157 -0.02882686 ... -0.36675575 -0.14722016
-0.22856475]
...
[ 0.07122967 0.07579704 0.2376182 ... -0.5245226 -0.38911286
-0.5513026 ]
[-0.05494669 -0.3587228 0.13438214 ... -0.6134821 -0.06194036
-0.46365416]
[-0.16560836 -0.15729778 0.00067104 ... -0.01925305 -0.3984945
0.12297624]]
[[-0.20293862 0.27669927 0.19337481 ... -0.14366734 0.06025359
-0.1156549 ]
[-0.02273261 0.20943424 0.26937988 ... -0.20701817 -0.03191033
0.03741883]
[ 0.16326293 0.19438037 0.12544776 ... -0.37406632 -0.1527986
-0.29249507]
...
[ 0.05573128 0.26873755 0.40287578 ... -0.65253705 -0.30244952
-0.68772614]
[-0.02555208 -0.0485841 0.19109009 ... -0.2797842 -0.01007691
-0.53623134]
[-0.30828896 0.04836991 -0.108813 ... -0.20583114 -0.40019956
0.11540392]]
問題在於您的損失,稀疏的分類交叉熵在這種情況下毫無意義,因為它用於分類,而且您似乎沒有分類問題。 要執行300維矢量的回歸,則均方誤差更有意義。
使用稀疏分類交叉熵的問題在於,這種損失假設模型輸出一個標量(一個元素向量),並在運行時對其進行檢查,並且此檢查失敗,這就是為什么會出現錯誤的原因。
另外,在回歸設置中,准確性沒有意義。
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='mean_squared_error')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.