2017-09-03 142 views
0

我正在研究预测二元选择的NN。当我尝试提取的预测,它不工作,并给了我这个跟踪:Tensorflow占位符具有负向尺寸

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"]()]] 

它看起来就像是从我定义我的“y”占位符来了,但我不能找出问题。

# 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[ ]: 

这里是执行“train_utils当代码:它只有当我试图得到一个orediction

这里是我的代码(它看起来奇怪,因为我从jupyter笔记本把它)发生。 create_network(X = X,重量=重量,偏差=偏见,神经元=神经元,层= num_layers,num_chunks = num_chunks,look_back = look_back)”行运行:

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 

预先感谢

回答

-1

由于此行发生错误print(sess.run(y, feed_dict={x: future_x})),您试图通过给另一个占位符x获取y。这里xy是独立的。

它应该被修正如下;您需要(你训练期间把标签是)喂相应阵列y

print(sess.run(y, feed_dict={y: y_test})) 
+0

什么?我试图通过使用X来预测Y.当我尝试喂X来获得Y时,喂养Y如何得到我Y? – tgs266

+0

如何预测使用另一个无关占位符的占位符的值? –

1

该预测由张量prediction给定(或output,因为它是一样的),而不是由占位符y 。预测代码应该是这样的:

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