word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))
for word, i in word_index.items():
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None: embedding_matrix[i] = embedding_vector
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix])(inp)
x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
x = GlobalMaxPool1D()(x)
x = Dense(16, activation="relu")(x)
x = Dropout(0.1)(x)
x = Dense(1, activation="sigmoid")(x)
model = Model(inputs=inp, outputs=x)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
While executing this block im getting this error
ValueError Traceback (most recent call last)
in
8
9 inp = Input(shape=(maxlen,))
---> 10 x = Embedding(max_features, embed_size, weights=[embedding_matrix])(inp)
11 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
12 x = GlobalMaxPool1D()(x)
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
73 if _SYMBOLIC_SCOPE.value:
74 with get_graph().as_default():
---> 75 return func(*args, **kwargs)
76 else:
77 return func(*args, **kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py in call(self, inputs, **kwargs)
466 # Load weights that were specified at layer instantiation.
467 if self._initial_weights is not None:
--> 468 self.set_weights(self._initial_weights)
469
470 # Raise exceptions in case the input is not compatible
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in eager_fn_wrapper(*args, **kwargs)
103 _SYMBOLIC_SCOPE.value = False
104 with context.eager_mode():
--> 105 out = func(*args, **kwargs)
106 finally:
107 _SYMBOLIC_SCOPE.value = prev_value
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py in set_weights(self, weights)
1124 str(pv.shape) +
1125 ' not compatible with '
-> 1126 'provided weight shape ' + str(w.shape))
1127 weight_value_tuples.append((p, w))
1128 K.batch_set_value(weight_value_tuples)
ValueError: Layer weight shape (50000, 300) not compatible with provided weight shape (0, 300)
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))
for word, i in word_index.items():
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None: embedding_matrix[i] = embedding_vector
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix])(inp)
x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
x = GlobalMaxPool1D()(x)
x = Dense(16, activation="relu")(x)
x = Dropout(0.1)(x)
x = Dense(1, activation="sigmoid")(x)
model = Model(inputs=inp, outputs=x)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
While executing this block im getting this error
ValueError Traceback (most recent call last)
in
8
9 inp = Input(shape=(maxlen,))
---> 10 x = Embedding(max_features, embed_size, weights=[embedding_matrix])(inp)
11 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
12 x = GlobalMaxPool1D()(x)
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
73 if _SYMBOLIC_SCOPE.value:
74 with get_graph().as_default():
---> 75 return func(*args, **kwargs)
76 else:
77 return func(*args, **kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py in call(self, inputs, **kwargs)
466 # Load weights that were specified at layer instantiation.
467 if self._initial_weights is not None:
--> 468 self.set_weights(self._initial_weights)
469
470 # Raise exceptions in case the input is not compatible
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in eager_fn_wrapper(*args, **kwargs)
103 _SYMBOLIC_SCOPE.value = False
104 with context.eager_mode():
--> 105 out = func(*args, **kwargs)
106 finally:
107 _SYMBOLIC_SCOPE.value = prev_value
C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py in set_weights(self, weights)
1124 str(pv.shape) +
1125 ' not compatible with '
-> 1126 'provided weight shape ' + str(w.shape))
1127 weight_value_tuples.append((p, w))
1128 K.batch_set_value(weight_value_tuples)
ValueError: Layer weight shape (50000, 300) not compatible with provided weight shape (0, 300)