1
votes

I'm having the following error with this code:

File "C:/Users/lourd/Desktop/Arquivos/PROGRAMAÇÂO/treino/IA", line 56, in module error, _ =sess.run([cost,optimizer],feed_dict={inputs: inp,targets:out})

File "C:\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 900, in run run_metadata_ptr)

File "C:\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1111, in _run str(subfeed_t.get_shape())))

ValueError: Cannot feed value of shape (4, 1) for Tensor 'input:0', which has shape '(?, 2)'

My code:

 import tensorflow as tf

# Espaços reservados

inputs = tf.placeholder('float', [None, 2], name='input')
targets = tf.placeholder('float', name='Target')

# Variaveis

weight1 = tf.Variable(tf.random_normal(shape= [2, 3], stddev=0.02, name='Weight1'))
biases1 = tf.Variable(tf.random_normal(shape= [3], stddev=0.02), name='Biases1')

# Multiplicador

hlayer = tf.matmul(inputs, weight1)
hlayer += biases1

# Função de ativação

hlayer = tf.sigmoid(hlayer, name='hAtivador')

# Camada oculta crie camadas de saida e conclua a rede

weight2 = tf.Variable(tf.random_normal(shape=[3, 1], stddev=0.02), name='Weight2')
biases2 = tf.Variable(tf.random_normal(shape=[1], stddev=0.02), name='Biases2')

# Camada de saida

output = tf.matmul(hlayer, weight2)
output += biases2
output = tf.sigmoid(output, name='outActivation')

# Optimização para treinar

cost = tf.squared_difference(targets, output)
cost = tf.reduce_mean(cost)
optimizer = tf.train.AdamOptimizer().minimize(cost)

# sessao TensotFlow

import numpy as np

inp = [[0.1], [0.2], [1.0],[1.1]]
out = [[0], [1], [1], [0]]

inp = np.array(inp)
out = np.array(out)

# Começar a sessão

epochs = 4000

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    for i in range(epochs):
        error, _ =sess.run([cost,optimizer],feed_dict={inputs: inp,targets:out})
        print(i,error)

# Teste

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    for i in range(epochs):
        error, _ = sess.run([cost, optimizer], feed_dict={inputs: inp, targets: out})
        print(i, error)
    while True:
        a = input('Primeira entrada: ')
        b = input('Segunda entrada: ')
        inp = [[a, b]]
        inp = np.array(inp)
        prediction = sess.run([output], feed_dict={inputs: inp})
        print(prediction)
1

1 Answers

1
votes

The shape of the data you feed to your placeholders is inconsistent with the shape of the placeholders.

inputs = tf.placeholder('float', [None, 2], name='input')

vs

inp = [[0.1], [0.2], [1.0],[1.1]]

Either change your inputs shape to be [None, 1] or add a second value for each entry of inp.