繁体   English   中英

Keras-使用Tensorflow后端的LSTM

[英]Keras - LSTM using Tensorflow Backend

我正在从这里尝试此示例代码。 之前我在使用Keras时遇到问题-Windows上的Theano现在刚迁移到Ubuntu上的Keras-Tensorflow。 版本是Keras(2.0.2)和TF(0.12.1)。

keras.__version__        # 2.0.2
tensorflow.__version__   # 0.12.1

import numpy
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset but only keep the top n words, zero the rest
top_words = 5000

(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=top_words)
# truncate and pad input sequences

max_review_length = 500

X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
# create the model

embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())

model.fit(X_train, y_train, nb_epoch=3, batch_size=64)
# Final evaluation of the model

scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))

错误如下,解决该错误的任何帮助将不胜感激

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework
  /tensor_util.py", line 302, in _AssertCompatible
  (dtype.name, repr(mismatch), type(mismatch).__name__))


**TypeError: Expected int32, got list containing Tensors of type '_Message' instead.**

您的代码在新版本的Tensorflow上运行良好(我在Tensorflow 1.0和Keras 2.0.2上进行了检查)。 您使用的是Tensorflow的旧版本。 请更新您的Tensorflow,它应该可以正常运行。 Tensorflow当前可用的版本是1.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM