我正在尝试为分类目的开发神经网络。这就是我所拥有的:
m, n = X_train.shape
alpha, epochs = 0.0035, 500
print(m) -> 26374
print(n) -> 75
X = tf.placeholder(tf.float32, [None, n])
Y = tf.placeholder(tf.float32, [None, 2])
W = tf.Variable(tf.zeros([n, 2]))
b = tf.Variable(tf.zeros([2]))
Y_hat = tf.nn.sigmoid(tf.add(tf.matmul(X, W), b))
cost = tf.nn.sigmoid_cross_entropy_with_logits(
logits = Y_hat, labels = Y)
optimizer = tf.train.GradientDescentOptimizer(
learning_rate = alpha).minimize(cost)
init = tf.global_variables_initializer()
# Starting the Tensorflow Session
x = X_train.values
y = y_train.values
with tf.Session() as sess:
# Initializing the Variables
sess.run(init)
# Lists for storing the changing Cost and Accuracy in every Epoch
cost_history, accuracy_history = [], []
# Iterating through all the epochs
for epoch in range(epochs):
cost_per_epoch = 0
# Running the Optimizer
sess.run(optimizer, feed_dict = {X : x, Y : y})
# Calculating cost on current Epoch
c = sess.run(cost, feed_dict = {X : x, Y : y})
# Calculating accuracy on current Epoch
correct_prediction = tf.equal(tf.argmax(Y_hat, 1),
tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,
tf.float32))
# Storing Cost and Accuracy to the history
cost_history.append(sum(sum(c)))
accuracy_history.append(accuracy.eval({X : x, Y : y}) * 100)
# Displaying result on current Epoch
if epoch % 100 == 0 and epoch != 0:
print("Epoch " + str(epoch) + " Cost: "
+ str(cost_history[-1]))
Weight = sess.run(W) # Optimized Weight
Bias = sess.run(b) # Optimized Bias
# Final Accuracy
correct_prediction = tf.equal(tf.argmax(Y_hat, 1),
tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,
tf.float32))
print("\nAccuracy:", accuracy_history[-1], "%")
我收到此错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-8d85989454ef> in <module>
15
16 # Running the Optimizer
---> 17 sess.run(optimizer, feed_dict = {X : x, Y : y})
18
19 # Calculating cost on current Epoch
~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
948 try:
949 result = self._run(None, fetches, feed_dict, options_ptr,
--> 950 run_metadata_ptr)
951 if run_metadata:
952 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1147 'which has shape %r' %
1148 (np_val.shape, subfeed_t.name,
-> 1149 str(subfeed_t.get_shape())))
1150 if not self.graph.is_feedable(subfeed_t):
1151 raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (26374, 1) for Tensor 'Placeholder_7:0', which has shape '(?, 2)'
我认为我需要根据此处的其他帖子使用reshape()
函数,但不确定如何使用。任何建议将不胜感激。我正在尝试按照教程进行操作,但是如果有人有其他建议的材料可以参考,请告诉我。
更新:
根据评论,我尝试过此操作
# Creating the One Hot Encoder
oneHot = OneHotEncoder(categories='auto')
# Encoding x_orig
oneHot.fit(X_train.values)
x = oneHot.transform(X_train.values).toarray()
# Encoding y_orig
oneHot.fit(Y_train.values)
y = oneHot.transform(Y_train.values).toarray()
并收到此错误:
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-44-6d9b8231b8b1> in <module>
4 # Encoding x_orig
5 oneHot.fit(X_train.values)
----> 6 x = oneHot.transform(X_train.values).toarray()
7
8 # Encoding y_orig
~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\scipy\sparse\compressed.py in toarray(self, order, out)
1022 if out is None and order is None:
1023 order = self._swap('cf')[0]
-> 1024 out = self._process_toarray_args(order, out)
1025 if not (out.flags.c_contiguous or out.flags.f_contiguous):
1026 raise ValueError('Output array must be C or F contiguous')
~\AppData\Local\Continuum\anaconda3\envs\tf-gpu\lib\site-packages\scipy\sparse\base.py in _process_toarray_args(self, order, out)
1184 return out
1185 else:
-> 1186 return np.zeros(self.shape, dtype=self.dtype, order=order)
1187
1188
MemoryError:
我不确定错误是什么或如何解决。