【问题标题】:How to model data for tensorflow?如何为张量流建模数据?
【发布时间】:2017-10-15 04:15:33
【问题描述】:

我有以下形式的数据:

A   B   C   D   E   F   G
1   0   0   1   0   0   1
1   0   0   1   0   0   1
1   0   0   1   0   1   0
1   0   1   0   1   0   0
...               
1   0   1   0   1   0   0
0   1   1   0   0   0   1
0   1   1   0   0   0   1
0   1   0   1   1   0   0
0   1   0   1   1   0   0

A,B,C,D 是我的输入,E,F,G 是我的输出。我使用 TensorFlow 在 Python 中编写了以下代码:

from __future__ import print_function
#from random import randint

import numpy as np
import tflearn
import pandas as pd


data,labels =tflearn.data_utils.load_csv('dummy_data.csv',target_column=-1,categorical_labels=False, n_classes=None)

 print(data)
 # Build neural network
 net = tflearn.input_data(shape=[None, 4])
 net = tflearn.fully_connected(net, 8)
 net = tflearn.fully_connected(net, 8)
 net = tflearn.fully_connected(net, 3, activation='softmax')
 net = tflearn.regression(net)

 # Define model
 model = tflearn.DNN(net)
 #Start training (apply gradient descent algorithm)
 data_to_array = np.asarray(data)
 print(data_to_array.shape)
 #data_to_array= data_to_array.reshape(6,9)
 print(data_to_array.shape)
 model.fit(data_to_array, labels, n_epoch=10, batch_size=3, show_metric=True)

我收到一条错误消息:

ValueError: 无法为张量 'InputData/X:0' 提供形状 (3, 6) 的值,其形状为 '(?, 4)'

我猜这是因为我的输入数据有 7 列 (0...6),但我希望输入层仅将前四列作为输入,并预测数据中的最后 3 列作为输出。我该如何建模?

【问题讨论】:

    标签: python python-3.x tensorflow neural-network tensor


    【解决方案1】:

    如果数据是 numpy 格式,那么前 4 列将采用简单的切片:

    data[:,0:4]
    

    : 表示“所有行”,0:4 是值的范围 0,1,2,3,前 4 列。

    如果数据不是 numpy 格式,只需将其转换为 numpy 格式,以便您轻松切片。

    这里有一篇关于 numpy 切片的相关文章:Numpy - slicing 2d row or column vector from array

    【讨论】:

    • 但是,我在哪里输入最后 3 列(E、F、G),它们是我的输出?
    • 你当然也需要为你的标签声明一个输入。
    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多