0
我最近開始使用tensorflow,這就像我的第二段代碼,我在設計這個神經網絡時被卡住了。我無法增加批量,這個問題一直存在。IndexError:數組索引太多
import numpy as np
import pandas as pd
import tensorflow as tf
import math
#importing the data and preprocessing it
dataset = pd.read_csv('C:\\Users\\Geeks_Sid\\Documents\\Deep-Learning-A-Z\Deep Learning A-Z\\Volume 1 - Supervised Deep Learning\\Part 1 - Artificial Neural Networks (ANN)\\Section 4 - Building an ANN\\Artificial_Neural_Networks\\Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
#creating a train test split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Creating layers for Neural network
n_nodes_hl1 = 1000
n_nodes_hl2 = 1000
n_nodes_hl3 = 1000
n_classes = 1
batch_size = 50
x = tf.placeholder('float', [None, 11])
y = tf.placeholder('float')
def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([11, n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes])),}
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']
print("I was in neural netowrk m")
return output
def train_neural_network(x):
prediction = neural_network_model(x)
# OLD VERSION:
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
# NEW:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
# OLD:
for epoch in range(hm_epochs):
epoch_loss = 0
current = 0
for _ in range(80):
currentprev = current
current += 100
epoch_x, epoch_y = tuple(X_train[:,currentprev:current]) ,tuple(y_train[:,currentprev:current])
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy:',accuracy.eval({x:X_test, y:y_test}))
#sess.run(tf.initialize_all_variables())
# NEW:
sess.run(tf.global_variables_initializer())
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
train_neural_network(x)
我正在處理看起來像這樣的錯誤。
train_neural_network(x)
I was in neural netowrk m
Traceback (most recent call last):
File "<ipython-input-8-7c7cbdae9b34>", line 1, in <module>
train_neural_network(x)
File "<ipython-input-7-b7e263fe7976>", line 20, in train_neural_network
epoch_x, epoch_y = tuple(X_train[:,currentprev:current]) ,tuple(y_train[:,currentprev:current])
IndexError: too many indices for array
我正在嘗試複製張量流MNIST數據集分類的代碼,他們在這裏使用下面這段代碼。我希望你能夠將這些代碼與我的代碼進行比較。如果有任何修改,請幫我
def train_neural_network(x):
prediction = neural_network_model(x)
# OLD VERSION:
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
# NEW:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
# OLD:
#sess.run(tf.initialize_all_variables())
# NEW:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(mnist.train.num_examples/batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy:',accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))
正如你看到的,我的代碼是非常相似的一個MNIST但我不能夠返回一個特定的元組是在這段代碼。
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
在此先感謝。如果你覺得這個問題是重複的,我想解釋一下,我找不到其他相關的東西。
」這是我的第二塊代碼,我堅持做一個神經網絡「耶穌隊友... –
我會'打印'你想索引的張量來獲得它的靜態形狀信息。基於這個錯誤,它聽起來像是一個標量或矢量。 –
我試過了,但沒用。 epoch_x和epoch_y期待別的東西。 – BlackSpecter