我试图用Keras在35条推文的语料库上训练语言模型。
我得到标题中提到的错误,并进行了以下追溯:
ValueError Traceback (most recent call last)
<ipython-input-101-5ed366712809> in <module>()
----> 1 create_model(X, Y, max_len, total_words)
<ipython-input-100-798dd17a8b2b> in create_model(predictors, label, max_sequence_len, total_words)
3
4 model = Sequential()
----> 5 model.add(Embedding(total_words, 10))
6 model.add(LSTM(150))
7 model.add(Dropout(0.1))
/usr/local/lib/python3.6/dist-packages/keras/models.py in add(self, layer)
495 # and create the node connecting the current layer
496 # to the input layer we just created.
--> 497 layer(x)
498
499 if len(layer._inbound_nodes[-1].output_tensors) != 1:
/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
590 '`layer.build(batch_input_shape)`')
591 if len(input_shapes) == 1:
--> 592 self.build(input_shapes[0])
593 else:
594 self.build(input_shapes)
/usr/local/lib/python3.6/dist-packages/keras/layers/embeddings.py in build(self, input_shape)
103 regularizer=self.embeddings_regularizer,
104 constraint=self.embeddings_constraint,
--> 105 dtype=self.dtype)
106 self.built = True
107
/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
411 if dtype is None:
412 dtype = K.floatx()
--> 413 weight = K.variable(initializer(shape),
414 dtype=dtype,
415 name=name,
/usr/local/lib/python3.6/dist-packages/keras/initializers.py in __call__(self, shape, dtype)
110 def __call__(self, shape, dtype=None):
111 return K.random_uniform(shape, self.minval, self.maxval,
—-> 112 dtype=dtype, seed=self.seed)
113
114 def get_config(self):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in random_uniform(shape, minval, maxval, dtype, seed)
3836 seed = np.random.randint(10e6)
3837 return tf.random_uniform(shape, minval=minval, maxval=maxval,
-> 3838 dtype=dtype, seed=seed)
3839
3840
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/random_ops.py in random_uniform(shape, minval, maxval, dtype, seed, name)
232 maxval = 1
233 with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) as name:
--> 234 shape = _ShapeTensor(shape)
235 minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
236 maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/random_ops.py in _ShapeTensor(shape)
41 else:
42 dtype = None
---> 43 return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
44
45
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
1009 name=name,
1010 preferred_dtype= preferred_dtype,
-> 1011 as_ref=False)
1012
1013
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1105
1106 if ret is None:
-> 1107 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1108
1109 if ret is NotImplemented:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
215 as_ref=False):
216 _ = as_ref
--> 217 return constant(v, dtype=dtype, name=name)
218
219
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
194 tensor_value.tensor.CopyFrom(
195 tensor_util.make_tensor_proto(
—> 196 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
197 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
198 const_tensor = g.create_op(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
443 """ - got shape %s, but wanted %s.""" %
444 (values, list(nparray.shape),
--> 445 _GetDenseDimensions(values)))
446
447 # python/numpy default float type is float64. We prefer float32 instead.
这是我所有的代码:
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding, LSTM, Dense, Dropout
from keras.preprocessing.text import Tokenizer
from keras.callbacks import EarlyStopping
from keras.models import Sequential
import keras.utils as ku
import numpy as np
def dataset_prep(data):
corpus = data
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
input_sequences = []
for line in corpus:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)
max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len,
padding='pre'))
predictors, next_word = input_sequences[:,:-1], input_sequences[:,-1]
label = ku.to_categorical(next_word, num_classes=total_words)
return input_sequences[:25], input_sequences[25:], max_sequence_len, list(input_sequences)
def create_model(predictors, label, max_sequence_len, total_words):
input_len = max_sequence_len
model = Sequential()
model.add(Embedding(total_words, 10))
model.add(LSTM(150))
model.add(Dropout(0.1))
model.add(Dense(total_words, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
history = model.fit(predictors, label, epochs=100, verbose=1)
print(history.history['loss'], history.history['val_loss'])
X, Y, max_len, total_words = dataset_prep(tweet_text)
create_model(X, Y, max_len, total_words)
我正在使用带有Keras v2.1.6和TensorFlow 1.9.0的Google Colaboratory
代码全部是带有Tesla K80 GPU的Python 3,用于运行时编译。