Keras输出形状不正确

时间:2019-04-23 23:46:53

标签: python tensorflow keras deep-learning classification

我正在建立一个递归神经网络来对英语或法语之间的单词进行分类,但是我得到一个关于输出形状不正确的错误。

我的网络看起来像这样:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
cu_dnnlstm_1 (CuDNNLSTM)     (None, 45, 128)           79872     
_________________________________________________________________
dropout_1 (Dropout)          (None, 45, 128)           0         
_________________________________________________________________
cu_dnnlstm_2 (CuDNNLSTM)     (None, 128)               132096    
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 258       
=================================================================

Total params: 212,226
Trainable params: 212,226
Non-trainable params: 0
_________________________________________________________________

这是我的错误: ValueError:检查目标时出错:预期density_1的形状为(1,),但数组的形状为(2,)

这是我的代码:

import pickle
import tensorflow as tf
import keras

from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.log_device_placement = 0

sess = tf.Session(config=config)
set_session(sess)

from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, CuDNNLSTM

import numpy as np

maxlen = 45
learning_rate = 0.002
characters = 26
empty = [0 for _ in range(characters)]
model_name = "language_predictor"
batch_size = 64
n_epoch = 3

with open("data_x.pickle", "rb") as file:
    x = pickle.load(file)

with open("data_y.pickle", "rb") as file:
    y = pickle.load(file)

for sequence in x:
    while len(sequence) < maxlen:
        sequence.append(empty)

x = np.array(x)
y = np.array(y)

model = Sequential()

model.add(CuDNNLSTM(128, input_shape=(maxlen, characters), return_sequences=True))
model.add(Dropout(0.2))

model.add(CuDNNLSTM(128))
model.add(Dropout(0.2))

model.add(Dense(2, activation='softmax'))

# Compile model
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=keras.optimizers.Adam(lr=0.002),
    metrics=['accuracy'],
)

model.summary()

model.fit(x,
          y,
          epochs=n_epoch,
          batch_size=batch_size,
          validation_split=0.1)

我的输入看起来像这样:[ [ [one hot array of letter], [more], [more] ], [another word] ]

我的输出如下:[ [0, 1 one hot array of language], [1, 0] ]

每当我运行程序时,都会出现此错误:

ValueError: Error when checking target:
expected dense_1 to have shape (1,) but got array with shape (2,)

尽管如此,它也应该起作用,因为输出层有2个神经元。

编辑: 将损失函数从“ sparse_categorical_crossentropy”更改为“ categorical_crossentropy”可以解决此问题。

0 个答案:

没有答案