1
votes

First time encounter such a problem.

The error is about feed_dict={tfkids: kids, tfkids_fit: kids_fit}, seems need to reshape kids_fit.

Can anyone help me with this problem?

import tensorflow as tf
from tensorflow.contrib.distributions import Normal
import numpy as np
import matplotlib.pyplot as plt

DNA_SIZE = 1
POP_SIZE = 10
LR = 0.1
N_GENERATION = 50

def F(x):
    return x**2

def get_fitness(value):
    return -value

mean = tf.Variable(tf.constant(13.), dtype=tf.float32)
sigma = tf.Variable(tf.constant(5.), dtype=tf.float32)
N_dist = Normal(loc=mean, scale=sigma)
make_kids = N_dist.sample([POP_SIZE])

tfkids = tf.placeholder(tf.float32, [POP_SIZE, DNA_SIZE])
tfkids_fit = tf.placeholder(tf.float32, [POP_SIZE])
loss = -tf.reduce_mean(N_dist.log_prob(tfkids) * tfkids_fit)
train_op = tf.train.GradientDescentOptimizer(LR).minimize(loss)

x = np.linspace(-20, 20, 100)
plt.plot(x, F(x))

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

plt.ion()
for g in range(N_GENERATION):
    kids = sess.run(make_kids)
    kids_fit = get_fitness(F(kids))
    sess.run(train_op, feed_dict={tfkids: kids, tfkids_fit: kids_fit})

    if "plot_points" in globals():
        plot_points.remove()

    plot_points = plt.scatter(kids, F(kids), s=30)
    plt.pause(0.05)

plt.ioff()
plt.show()

This gives an error when attempting to test code.

ValueError: Cannot feed value of shape (10,) for Tensor 'Placeholder:0', which has shape '(10, 1)'

3

3 Answers

1
votes

Your Placeholder:0 is tfkids = tf.placeholder(tf.float32, [POP_SIZE, DNA_SIZE]).

As you can see, tfkids shape is [POP_SIZE, DNA_SIZE] = (10, 1).

Your kids variable, instead, has shape = (10).

Although both shapes contain 10 values, the first has 2 dimensions while the second is 1 D.

You have to, thus, expand the dimension of your kids variable in order to be compatible with tfkids in this way:

sess.run(train_op, feed_dict={tfkids: np.expand_dims(kids, axis=1), tfkids_fit: kids_fit})

np.expand_dims allows you to add a 1D dimension to your kids shape

1
votes

You can reshape the kids tensor.

kids = sess.run(make_kids)
kids = tf.reshape(kids,(None,1))
kids_fit = get_fitness(F(kids))
sess.run(train_op, feed_dict={tfkids: kids, tfkids_fit: kids_fit})
1
votes

Problem: When you declared your tfkids variable you specify it's shape to [POP_SIZE, DNA_SIZE] which is (10, 1). But when you feeding real data into the placeholder during training you are passing a (10,) shaped data.

Solution: So you have to reshape your training data to (10, 1) in order to feed it to the variable. There are a couple of ways you can reshape your data. You can use reshape function of numpy library. Do the following before feeding you training data.

kids = np.reshape(kids, [-1, 1])

Hope this helps!