Python TFLearn必须完全定义新变量(Conv2D / W)的形状,而是(1,1,?,32)

时间:2017-08-29 00:53:50

标签: python tensorflow tflearn

您好我正在尝试制作预测CNN的股票,因为我想挑战自我,我突然发现这个错误它认为Convd2D没有完全定义但我不知道还有什么可以解决这个问题错误。有人可以帮我这里是我的代码

import tflearn
import numpy as np
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import csv
import math

prices = []
templist = []
X = []
Y = []
i=0

def get_data(filename):
    with open(filename, 'r') as csvfile:
        csvFileReader = csv.reader(csvfile)
        next(csvFileReader)
        for row in csvFileReader:
            prices.append(float(row[1]))
    return

get_data('intc.csv')
i=len(prices)-5
while i>5:
    X.append(prices[i-5:i])
    Y.append(prices[i-6])
    i-=1



#X, Y, test_x, test_y = mnist.load_data(one_hot=True)

X = np.reshape(X,(-1,len(X),len(X[0]),1))

convnet = input_data(shape=[None, 241, 5, None], name='input')

convnet = conv_2d(convnet, 32, 1, activation='relu')
convnet = max_pool_2d(convnet, 1)

convnet = conv_2d(convnet, 64, 1, activation='relu')
convnet = max_pool_2d(convnet, 1)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 1, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet)
model.fit({'input': X}, {'targets': Y}, n_epoch=2, snapshot_step=500, show_metric=True)
model.save('quicktest.model')

我得到的错误是

File "C:\Users\User\Desktop\Python Projects\Intel_Stock_Price_Prediction.py", line 38, in <module>
    convnet = conv_2d(convnet, 32, 1, activation='relu')
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tflearn\layers\conv.py", line 85, in conv_2d
    restore=restore)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\contrib\framework\python\ops\arg_scope.py", line 181, in func_with_args
    return func(*args, **current_args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tflearn\variables.py", line 65, in variable
    validate_shape=validate_shape)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 367, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 685, in _get_single_variable
    "but instead was %s." % (name, shape))
ValueError: Shape of a new variable (Conv2D/W) must be fully defined, but instead was (1, 1, ?, 32).

有人可以帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

这里的错误与input_data函数有关。您应该完全定义输入数据形状。您可以将None用于批量维度,但不能用于任何其他输入维度。

# replace this line
convnet = input_data(shape=[None, 241, 5, None], name='input')
# hopefully with your correct input dimension. you need provide a value
# in your case
num_channels =1
convnet = input_data(shape=[None, 241, 5, num_channels], name='input')