Tensorflow占位符具有负维度

时间:2017-09-03 18:01:32

标签: python machine-learning tensorflow

我正在研究预测二元选择的NN。当我尝试提取预测时,它不起作用,并给我这个痕迹:

2017-09-03 13:52:59.302796: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1,2] has negative dimensions
2017-09-03 13:52:59.302843: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
2017-09-03 13:52:59.302922: W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1,2] has negative dimensions
2017-09-03 13:52:59.302939: E tensorflow/core/common_runtime/executor.cc:644] Executor failed to create kernel. Invalid argument: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Traceback (most recent call last):
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1139, in _do_call
    return fn(*args)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1121, in _run_fn
    status, run_metadata)
  File "/home/tucker/anaconda3/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "train.py", line 104, in <module>
    print(sess.run(y, feed_dict={x: future_x}))
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'Placeholder_1', defined at:
  File "train.py", line 37, in <module>
    y = tf.placeholder("float32", [None, num_classes])
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1530, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1954, in _placeholder
    name=name)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/tucker/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Shape [-1,2] has negative dimensions
     [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

看起来它来自我定义我的&#34; y&#34;占位符,但我无法弄清楚这个问题。它只发生在我试图得到一个铭文时

这是我的代码(它看起来很奇怪,因为我从一个jupyter笔记本中取出它):

# coding: utf-8

# # Imports

# In[1]:

from config import train
from config.data_parameters import data_params
from utils import train_utils
import tensorflow as tf
import numpy as np

sess = tf.InteractiveSession()
#get_ipython().magic('load_ext autoreload')
#get_ipython().magic('autoreload 2')


# # Get Data

# In[2]:

train_x, train_y, test_x, test_y, future_x = train_utils.get_shelve()
print (future_x)

# # Begin TF Stuff

# ## Initialize Vars

# In[3]:

num_chunks = len(train_x[0])
look_back = data_params['look_back']
num_classes = len(test_y[0])

x = tf.placeholder("float32", [None, num_chunks, look_back])
y = tf.placeholder("float32", [None, num_classes])


# ## Create Weights and Biases

# In[4]:

neurons = train.network['neurons']
weights = {
    'layer':tf.Variable(tf.random_normal([neurons, num_classes])),
}
biases = {
    'layer':tf.Variable(tf.random_normal([num_classes])),
}
print (num_chunks)


# ## Create Network

# In[5]:

num_layers = train.network['layers']
output = train_utils.create_network(x = x,
                                    weights = weights,
                                    biases = biases, 
                                    neurons = neurons, 
                                    layers = num_layers, 
                                    num_chunks = num_chunks, 
                                    look_back = look_back)


# ## The Rest

# In[6]:

prediction = output
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = prediction, labels = y))
optimizer = tf.train.AdamOptimizer(train.parameters['learning_rate']).minimize(loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))


# In[8]:

batch_size = train.parameters['batch_size']

sess.run(tf.initialize_all_variables())

for epoch in range(train.parameters['epochs']):
    epoch_loss = 0
    i=0
    accs = []
    while i < len(train_x):
        start = i
        end = i+batch_size
        batch_x = np.array(train_x[start:end])
        batch_y = np.array(train_y[start:end])
        _, c, acc = sess.run([optimizer, loss, accuracy], feed_dict={x: batch_x,
                                                                     y: batch_y})
        accs.append(acc)
        epoch_loss += c
        i+=batch_size
    print ("Epoch %s completed out of %s | Loss: %s | Acc: %s" % (epoch + 1, 
                                                                  train.parameters['epochs'],
                                                                  epoch_loss,
                                                                  np.mean(accs)))

print(sess.run(y, feed_dict={x: future_x}))



# In[ ]:




# In[ ]:

以下是&#34; train_utils.create_network(x = x,权重=权重,偏差=偏差,神经元=神经元,图层= num_layers,num_chunks = num_chunks,look_back = look_back)&#34时执行的代码;行运行:

def create_network(x, weights, biases, neurons, layers, num_chunks, look_back):
    # x: tf var
    # weights: weights defined in file
    # biases: biases defined in file
    # num_chunks: num_chunks defined in file
    # look_back: look_back defined in file

    #return: idk just take it
    x = tf.transpose(x, [1,0,2])
    x = tf.reshape(x, [-1, look_back])
    x = tf.split(x, num_chunks, 0)

    cell = rnn.LSTMCell(neurons, state_is_tuple=True)

    def lstm_cell():
        return rnn.LSTMCell(neurons, state_is_tuple=True)
    stacked_lstm = rnn.MultiRNNCell([lstm_cell() for _ in range(layers)])

    outputs, states = rnn.static_rnn(cell, x, dtype=tf.float32)

    output = tf.matmul(outputs[-1], weights['layer']) + biases['layer']

    return output

提前致谢

2 个答案:

答案 0 :(得分:2)

预测由张量prediction(或output给出,因为它是相同的),而不是占位符y(在训练期间放置标签的位置) 。预测代码应该是这样的:

print(sess.run(prediction, feed_dict={x: future_x}))

答案 1 :(得分:0)

由于此行print(sess.run(y, feed_dict={x: future_x}))而导致错误,您试图通过提供另一个占位符y来获取x。此处xy是独立的。

应纠正如下;您需要为y

提供相应的数组
print(sess.run(y, feed_dict={y: y_test}))