python - 如何为 tensorflow 建模数据?

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

我有以下形式的数据:

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: Cannot feed value of shape (3, 6) for Tensor 'InputData/X:0', which has shape '(?, 4)'

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

最佳答案

如果数据采用 numpy 格式,则前 4 列将通过简单的切片获取:

data[:,0:4]

: 表示“所有行”,0:4 是一系列值 0,1,2,3,前 4 列。

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

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

关于python - 如何为 tensorflow 建模数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43995397/

相关文章:

python - 将 TensorFlow 神经网络编码到 tkinter GUI 中

python - 如何将电压读数限制在写入日志文件的范围内?

python - 如何从Python Tkinter应用程序捕获到控制台的任何输出?

python-3.x - TypeError : iteration over a 0-d array, 使用 numpy

python - 通过磁条索引输入信用卡

Linux, python 3 : syntax error on commandline but not in ipython %run command

python - Tensorflow:如何在使用新的最终层扩展图形后恢复初始 v3 预训练网络权重

python - pandas 系列上的 numpy diff

python - 如何在 Pandas/Numpy 中使用 dateOffset 对日内时间序列数据进行重采样?

tensorflow - Tensorflow 分类编码中多余列的逻辑是什么?