2017-09-03 90 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}))