Keras中的LSTM出错

时间:2017-09-26 23:21:25

标签: python tensorflow keras lstm

我有一个用于文本分类的LSTM代码:

class SentimentLstm(object):

    # Class for Sentiment Classification using LSTM's cells

    def __init__(self,x_train,y_train,x_test,y_test,len_vocab,num_lstm_units=50):

        # Contructor to initialize the attributes

        # Reshaping the original training images from 3D to 2D. 
        # Training data
        self.x_train=x_train
        #Normalise
        # Test data
        self.x_test=x_test
        #Normalise
        # Converting the Labels to One-hot Encoding
        self.y_train=y_train
        self.y_test=y_test

        self.len_vocab=len_vocab

        #No. of LSTM units
        self.num_units=num_lstm_units

    def model_param(self): 

        # Method to do deep learning

        from keras.models import Sequential
        from keras.layers import Dense, Flatten, Dropout, Activation
        from keras.layers import LSTM
        from keras.layers.embeddings import Embedding
        from keras.initializers import TruncatedNormal

        tn=TruncatedNormal(mean=0.0, stddev=1/sqrt(self.x_train.shape[1]*self.x_train.shape[1]), seed=2)

        self.model = Sequential()
        self.model.add(Embedding(self.len_vocab,300,input_length=self.x_train.shape[1]))

        # Adding LSTM cell
        self.model.add(LSTM(self.num_units,dropout=0.30,kernel_initializer=tn,name="lstm_1"))
        # Adding the dense output layer for Output
        self.model.add(Dense(1,activation="sigmoid",name="output_layer"))

        #sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
        self.model.compile(loss='binary_crossentropy',
              optimizer="adam",
              metrics=['accuracy'])

        self.model.summary()

    def fit(self):
        # Training the deep learning network on the training data

        # Adding the callbacks for Logging 

        import keras
        logger_tb=keras.callbacks.TensorBoard(
        log_dir="logs_sentiment_lstm",
        write_graph=True,
        histogram_freq=5
        )

        self.model.fit(self.x_train, self.y_train,validation_split=0.20,
          epochs=10,
          batch_size=128,callbacks=[logger_tb]
        )

我的训练数据形状是:

Out[29]:
(17500, 200)

有17500个文本数据样本转换为字整数,最大序列长度为200.

我的模型摘要:

Layer (type)                 Output Shape              Param #   
=================================================================
embedding_1 (Embedding)      (None, 200, 300)          22221600  
_________________________________________________________________
lstm_1 (LSTM)                (None, 50)                70200     
_________________________________________________________________
output_layer (Dense)         (None, 1)                 51        
=================================================================
Total params: 22,291,851
Trainable params: 22,291,851
Non-trainable params: 0
_______________________________________

但是当我运行此操作时,我收到错误invalidArgument

/usr/local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
    465           compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466           pywrap_tensorflow.TF_GetCode(status))
    467   finally:

InvalidArgumentError: You must feed a value for placeholder tensor 'embedding_layer_input' with dtype float
     [[Node: embedding_layer_input = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
     [[Node: output_layer_2/bias/read/_237 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_1546_output_layer_2/bias/read", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

编辑:

初始化SentimentLstm对象。

### Instantiting the class object
sent_lstm=SentimentLstm(x_train,y_train,x_test,y_test,len(vocab))
## Normalizing and Standarizing the data
​
sent_lstm.model_param()
​
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_layer (Embedding)  (None, 200, 300)          22221600  
_________________________________________________________________
lstm_1 (LSTM)                (None, 50)                70200     
_________________________________________________________________
output_layer (Dense)         (None, 1)                 51        
=================================================================
Total params: 22,291,851.0
Trainable params: 22,291,851
Non-trainable params: 0.0

0 个答案:

没有答案