2017-03-27 105 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