Python 简单前馈神经网络-索引器:索引59超出大小为47的轴0的界限
我正在尝试用tensorflow学习机器学习。我没有使用现有的iris数据,而是使用自己收集的赛马数据Python 简单前馈神经网络-索引器:索引59超出大小为47的轴0的界限,python,machine-learning,tensorflow,Python,Machine Learning,Tensorflow,我正在尝试用tensorflow学习机器学习。我没有使用现有的iris数据,而是使用自己收集的赛马数据 # Implementation of a simple MLP network with one hidden layer. Tested on the iris data set. # Requires: numpy, sklearn>=0.18.1, tensorflow>=1.0 # NOTE: In order to make the code simple, we r
# Implementation of a simple MLP network with one hidden layer. Tested on the iris data set.
# Requires: numpy, sklearn>=0.18.1, tensorflow>=1.0
# NOTE: In order to make the code simple, we rewrite x * W_1 + b_1 = x' * W_1'
# where x' = [x | 1] and W_1' is the matrix W_1 appended with a new row with elements b_1's.
# Similarly, for h * W_2 + b_2
import tensorflow as tf
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import pandas as pd
RANDOM_SEED = 42
tf.set_random_seed(RANDOM_SEED)
def init_weights(shape):
""" Weight initialization """
weights = tf.random_normal(shape, stddev=0.1)
return tf.Variable(weights)
def forwardprop(X, w_1, w_2):
"""
Forward-propagation.
IMPORTANT: yhat is not softmax since TensorFlow's softmax_cross_entropy_with_logits() does that internally.
"""
h = tf.nn.sigmoid(tf.matmul(X, w_1)) # The \sigma function
yhat = tf.matmul(h, w_2) # The \varphi function
return yhat
def get_iris_data():
df = pd.read_csv('export.csv')
df = df[['track_id','distance','participant','horse_id','horse_money','horse_is_home','age','trainer_id','start_position','galloped','disqualified','jockey_id','jockey_is_home','shoes_back','shoes_front','finish_position']]
df.fillna(52, inplace=True)
data = np.array(df.drop(['finish_position'],1))
target = np.array(df['finish_position']).astype(int)
# Prepend the column of 1s for bias
N, M = data.shape
all_X = np.ones((N, M + 1))
all_X[:, 1:] = data
# Convert into one-hot vectors
num_labels = len(np.unique(target))
all_Y = np.eye(num_labels)[target] # One liner trick!
return train_test_split(all_X, all_Y, test_size=0.33, random_state=RANDOM_SEED)
def main():
train_X, test_X, train_y, test_y = get_iris_data()
# Layer's sizes
x_size = train_X.shape[1] # Number of input nodes: 4 features and 1 bias
h_size = 256 # Number of hidden nodes
y_size = train_y.shape[1] # Number of outcomes (3 iris flowers)
# Symbols
X = tf.placeholder("float", shape=[None, x_size])
y = tf.placeholder("float", shape=[None, y_size])
# Weight initializations
w_1 = init_weights((x_size, h_size))
w_2 = init_weights((h_size, y_size))
# Forward propagation
yhat = forwardprop(X, w_1, w_2)
predict = tf.argmax(yhat, axis=1)
# Backward propagation
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=yhat))
updates = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
# Run SGD
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for epoch in range(100):
# Train with each example
for i in range(len(train_X)):
sess.run(updates, feed_dict={X: train_X[i: i + 1], y: train_y[i: i + 1]})
train_accuracy = np.mean(np.argmax(train_y, axis=1) ==
sess.run(predict, feed_dict={X: train_X, y: train_y}))
test_accuracy = np.mean(np.argmax(test_y, axis=1) ==
sess.run(predict, feed_dict={X: test_X, y: test_y}))
print("Epoch = %d, train accuracy = %.2f%%, test accuracy = %.2f%%"
% (epoch + 1, 100. * train_accuracy, 100. * test_accuracy))
sess.close()
if __name__ == '__main__':
main()
当我尝试在python中运行此操作时,我遇到了一个错误:
Traceback (most recent call last):
File "test.py", line 94, in <module>
main()
File "test.py", line 50, in main
train_X, test_X, train_y, test_y = get_iris_data()
File "test.py", line 46, in get_iris_data
all_Y = np.eye(num_labels)[target] # One liner trick!
IndexError: index 59 is out of bounds for axis 0 with size 47
回溯(最近一次呼叫最后一次):
文件“test.py”,第94行,在
main()
文件“test.py”,第50行,在main中
训练X,测试X,训练y,测试y=获取iris\u数据()
get_iris_数据中第46行的文件“test.py”
all_Y=np.eye(num_labels)[target]#一行技巧!
索引器:索引59超出大小为47的轴0的界限
有人能帮我做错事吗?或者给我一个提示,提前谢谢
这是我导出的数据,有50000行。
您正试图抓住列表中只有47个元素的第59个元素。调试以找出.num_labels是唯一目标数的原因。是否所有目标都在[0,num_-1]范围内?看起来有59个目标,但只有47个不同的目标。您可以分享您的目标吗?我上传了一个导出数据示例,其中有50000行而不是我的600000行,我遇到了相同的问题,但改为“IndexError:index 44超出了大小为18的轴0的界限”。