tensorflow invalidargumenterror: matrix size-compatible: in[0]: [100,784], in[1]: [500,10]

  • Last Update :
  • Techknowledgy :

One error lies in this line

 l2 = tf.add(tf.matmul(data, hidden_2_layer['weights']), hidden_2_layer['biases'])

Your second weights variable has dimensions 500 x 500, but your data variable was fed with data 100x784 so multiplication is incompatible. Make this,

 l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])

Always specify a shape for the placeholder, like this,

x = tf.placeholder(tf.float32, shape = (None, 784))

Suggestion : 2

Hi all, I tried to use CRF layer on top of Bi-LSTM-CNN NER model on this implementation https://github.com/kamalkraj/Named-Entity-Recognition-with-Bidirectional-LSTM-CNNs,By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails., Pricing Plans Compare plans Contact Sales Education , In this organization All GitHub ↵ Jump to ↵

wordEmbeddings = wordEmbeddings.reshape((wordEmbeddings.shape[0], 300))
words_input = Input(shape = (None, ), dtype = 'int32', name = 'words_input')
words = Embedding(input_dim = wordEmbeddings.shape[0], output_dim = wordEmbeddings.shape[1], weights = [wordEmbeddings], trainable = False)(words_input)
casing_input = Input(shape = (None, ), dtype = 'int32', name = 'casing_input')
casing = Embedding(output_dim = caseEmbeddings.shape[1], input_dim = caseEmbeddings.shape[0], weights = [caseEmbeddings], trainable = False)(casing_input)
character_input = Input(shape = (None, 52, ), name = 'char_input')
embed_char_out = TimeDistributed(Embedding(len(char2Idx), 30, embeddings_initializer = RandomUniform(minval = -0.5, maxval = 0.5)), name = 'char_embedding')(character_input)
dropout = Dropout(0.5)(embed_char_out)
conv1d_out = TimeDistributed(Conv1D(kernel_size = 3, filters = 30, padding = 'same', activation = 'tanh', strides = 1))(dropout)
maxpool_out = TimeDistributed(MaxPooling1D(52))(conv1d_out)
char = TimeDistributed(Flatten())(maxpool_out)
char = Dropout(0.5)(char)
output = concatenate([words, casing, char])
output = Bidirectional(LSTM(200, return_sequences = True, dropout = 0.50, recurrent_dropout = 0.25))(output)
output = TimeDistributed(Dense(100, activation = 'softmax'))(output)
print(len(label2Idx))
crf = CRF(len(label2Idx))
output = crf(output)

model = Model(inputs = [words_input, casing_input, character_input], outputs = [output])
model.compile(loss = crf.loss_function, optimizer = 'adam', metrics = [crf.accuracy])

for epoch in range(epochs):
   print("Epoch %d/%d" % (epoch, epochs))
a = Progbar(len(train_batch_len))
for i, batch in enumerate(iterate_minibatches(train_batch, train_batch_len)):
   labels, tokens, casing, char = batch
print(labels.shape, tokens.shape, casing.shape, char.shape)
model.train_on_batch([tokens, casing, char], labels)
a.update(i)
a.update(i + 1)
print(' ')
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
   (0) Invalid argument: Matrix size - incompatible: In[0]: [0, 1], In[1]: [70, 70]
   [
      [{
         {
            node loss / crf_1_loss / MatMul_1
         }
      }]
   ]
   [
      [metrics / crf_viterbi_accuracy / strided_slice_10 / _229]
   ]
   (1) Invalid argument: Matrix size - incompatible: In[0]: [0, 1], In[1]: [70, 70]
   [
      [{
         {
            node loss / crf_1_loss / MatMul_1
         }
      }]
   ]
 CRF(self.n_class, sparse_target = True)

Suggestion : 3

Answer at: 2016-12-28 Accepted Vote count: 6 , Answer at: 2016-12-29 Accepted Vote count: 6 , Answer at: 2016-12-29 Accepted Vote count: 11 , Answer at: 2016-12-13 Accepted Vote count: 7


import tensorflow as tf

index = tf.argmax(one_hot_vector, axis = 0)

# Assume global `begin`, `end`
and `stride`
def iterate(active, dim): if dim == len(begin): # last dimension incremented, work on the new matrix # Note that `active`
and `begin`
are lists new_matrix[active - begin] = old_matrix[active]
else: for i in range(begin[dim], end[dim], stride[dim]): new_active = copy(active) new_active[dim] = i iterate(new_active, dim + 1) iterate(begin, 0)

import tensorflow.contrib.learn as skflow from sklearn
import datasets, metrics iris = datasets.load_iris() # made a change in the next line classifier = skflow.DNNClassifier(hidden_units = [10, 20, 10], n_classes = 3) classifier.fit(iris.data, iris.target) score = metrics.accuracy_score(iris.target, classifier.predict(iris.data)) print("Accuracy: %f" % score)

 l2 = tf.add(tf.matmul(data, hidden_2_layer['weights']), hidden_2_layer['biases'])

cell = tf.nn.rnn_cell.LSTMCell(num_hidden, state_is_tuple = True) input_single = tf.ones([batch_size, input_size]) state_single = cell.zero_state(batch_size, tf.float32)(output_single, state_single) = cell(input_single, state_single)