tensorflow feed_dict重塑列表

时间:2018-08-02 12:10:15

标签: python arrays list csv tensorflow

  

我该如何解决?请帮助...我是新来的。我该怎么办?

  • 这是我的想法:
    1. 将价格列表制作为数组列表?但是怎么办?
    2. 调整图层尺寸的值?最好的价值是什么?
  

关于这个项目

  • 从csv文件中获取26个数据以馈入网络,以便网络    预测第二天的价格。

代码:

import csv
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import tensorflow as tf
date = []
price = []
tdate = []
tprice = []
def get_data(filename):
    with open('TSLA.csv', 'r') as csvfile:
        csvR = csv.reader(csvfile)
        next(csvR)
        for c in range(p):
            next(csvR)
        for i,row in enumerate(csvR):
            date.append(datetime.strptime(row[0],'%m/%d/%Y'))
            price.append(float(row[5]))
            if(i >= 26):
                tdate.append(datetime.strptime(row[0],'%m/%d/%Y'))
                tprice.append(float(row[5]))
                date.pop()
                price.pop()
                break
def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs
xs = tf.placeholder(tf.float32, [None, 26])
ys = tf.placeholder(tf.float32, [None, 1])
l1 = add_layer(xs, 26, 9, activation_function=tf.nn.relu)
prediction = add_layer(l1, 9, 1, activation_function=None)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for p in range(500):                
    get_data('TSLA.csv')
    for list in zip(date,price):
        print (list)
    print(tdate,tprice)
    sess.run(train_step, feed_dict={xs: np.expand_dims(price,axis=26), ys: tprice})
    prediction_value = sess.run(prediction, feed_dict={xs: price})
    plt.plot_date(tdate, prediction_value, fmt="b-")
    plt.plot_date(date, price, fmt="r-")
    plt.scatter(tdate, tprice)
    plt.ion()
    plt.show()
    plt.pause(0.2)
    del date[:]
    del price[:]
    del tdate[:]
    del tprice[:]

错误:

Traceback (most recent call last):
  File "modelA.py", line 48, in <module>
    sess.run(train_step, feed_dict={xs: np.expand_dims(price,axis=26), ys: tprice})
  File "C:\p35\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "C:\p35\lib\site-packages\tensorflow\python\client\session.py", line 1111, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (26, 1) for Tensor 'Placeholder:0', which has shape '(?, 26)'

1 个答案:

答案 0 :(得分:-1)

在第48行尝试以下操作:

{{1}}

np.expand_dims扩展数组的形状。您的初始价格数组的形状为(26,)。使用axis = 0将形状更改为(1,26)。