2017-03-27 104 views
1

我正在试着从here获取此示例代码。之前我在使用Keras时遇到了问题 - Windows上的Theano现在只是迁移到了Ubuntu上的Keras-Tensorflow。版本是Keras(2.0.2)和TF(0.12.1)。Keras - 使用Tensorflow后端的LSTM

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.** 
+1

尝试将TF升级到最新版本1.0.x :) –

+1

您是否成功使用Tensorflow运行任何程序?或者是你从Theano搬出来后第一次尝试运行某些东西? –

+0

@LiorMagen我可以独立处理张量流。 –

回答

0

你的代码是在Tensorflow的新版本运行正常(我检查它Tensorflow 1.0和Keras 2.0 0.2)。您正在使用旧版本的Tensorflow。请更新您的Tensorflow,它应该完美地工作。当前可用的Tensorflow版本是1.1