错误:
TypeError: while_loop() got an unexpected keyword argument 'maximum_iterations'
代码:
# coding: utf-8
# https://kexue.fm/archives/4293
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense, GRU
from keras.datasets import imdb
from keras import backend as K
margin = 0.6
theta = lambda t: (K.sign(t)+1.)/2.
max_features = 20000
maxlen = 80
batch_size = 32
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
model = Sequential()
model.add(Embedding(max_features, 128))
model.add(GRU(units=128, dropout=0.2, recurrent_dropout=0.2, ))
model.add(Dense(1, activation='sigmoid'))
def loss(y_true, y_pred):
return - (1 - theta(y_true - margin) * theta(y_pred - margin)
- theta(1 - margin - y_true) * theta(1 - margin - y_pred)
) * (y_true * K.log(y_pred + 1e-8) + (1 - y_true) * K.log(1 - y_pred + 1e-8))
model.compile(loss=loss,
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=15,
validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test,
batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)
解决方法:
Use: Keras 2.1.2 Tensorflow 1.4.1
pip install keras==2.1.2
pip install tensorflow-gpu==1.4.0