简体   繁体   中英

In[0] is not a matrix. Instead it has shape [100] [Op:MatMul]

I'm new to tensorflow and I'm trying to implement a simple neural network for NLP and I'm getting an error that I can't figure out how to fix. The code for my network is here:

model = tf.keras.Sequential()
model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
model.add(layers.BatchNormalization())
model.add(layers.ReLU())

model.add(layers.Dense(7*7*256, use_bias=False))
model.add(layers.BatchNormalization())
model.add(layers.ReLU())

model.add(layers.Dense(7*7*128, use_bias=False))
model.add(layers.BatchNormalization())
model.add(layers.ReLU())

model.add(layers.Dense(7*7*64, use_bias=False))
model.add(layers.BatchNormalization())
model.add(layers.ReLU())

model.add(layers.Dense(7*7*64, use_bias=False))
model.add(layers.BatchNormalization())
model.add(layers.ReLU())

model.add(layers.Dense(100, use_bias = False, activation="relu"))

the input shape is (100,). I'm trying to input a tensor of 100 floats (ascii characters normalized between 0 and 1) as shown in this code:

generator = make_generator_model()

human_array = tf.convert_to_tensor([human[0]])
print("human shape: " + str(tf.shape(human[0])))
noise = tf.random.normal([1, 100])
print("noise shape: " + str(tf.shape(noise)))
generated_response_test = generator(human[0], training=False)

print("m: " + chr(109))
print(generated_response_test)
print(get_message(np.round(generated_response_test[0] * 95 + 32)))

where human[0] is the tensor I'm talking about

and I get the following error, as well as this warning:

WARNING:tensorflow:Model was constructed with shape (None, 100) for input
Tensor("dense_input:0", shape=(None, 100), dtype=float32), but it was
called on an input with incompatible shape (100,).

In[0] is not a matrix. Instead it has shape [100] [Op:MatMul]

interestingly, the code works with no warnings or errors when I pass in random noise from a normal distribution with the same shape. the outputs of tf.shape(noise) and then tf.shape(human[0]) are:

human shape: tf.Tensor([  1 100], shape=(2,), dtype=int32)
noise shape: tf.Tensor([  1 100], shape=(2,), dtype=int32)

So I am very confused

I'm fairly sure that this is an issue with the way I defined my tensor variable and that matmul() requires a matrix as input, but I can't for the life of me figure out how to convert my list into the proper format and I can't find anything on the internet that addresses my problem

Can anyone offer any help? Thanks!

I was able to solve this problem by specifying shape = [1,100] rather than shape = (100,) in tf.variable and I moved the []'s to inside the input so my working code was

generator = make_generator_model()

human_input = tf.Variable([np.array(human[0])], dtype = tf.float32, shape = [1,100])
human_input = tf.random.normal([1, 100])
generated_response = generator(human_input, training=False)

print("m: " + chr(109))
print(generated_response)
print(print_message(np.round(generated_response[0] * 95 + 32)))

notice the differences in my definition of human_input. Hope this helps anyone that also has the issue, and comment on this answer if you need more info!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM