您好,让我首先说一下我的StackOverflow经验与我的编程经验非常有限。不用说,如果我以StackOverflow的格式或我的代码格式出现任何无意的错误,我道歉并欢迎编辑我的帖子。
我做了大量的研究,所以不要创建重复的帖子,比如this one,但无论我怎么努力,我要么无法从帖子中获得意义,要么对如何实施的知识太少它
所以这是我的问题:
我正在做类似于情绪分析的机器学习算法,我使用Sklearns CountVectorizer和Tfidf在大约6,000条推文上,这转化为以下
<6612x9703 sparse matrix of type '<class 'numpy.float64'>
with 47719 stored elements in Compressed Sparse Row format>
从那里我尝试使用以下功能训练它:
X = tf.placeholder("float", [None, X_train_spmx.shape[0]])
y = tf.placeholder("float")
def train_neural_net(x):
prediction = neural_net_model(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
i = 0
while i < len(y_train):
start = i
end = i+batch_size
batch_x = np.array(X_train_spmx[start:end])
print(batch_x)
batch_y = np.array(y_train[start:end])
print(batch_y)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_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}))
train_neural_net(X)
然而,当我跑步时,我得到错误:
ValueError Traceback (most recent call
last)
<ipython-input-16-8130c429fbca> in <module>()
32 print('Accuracy:',accuracy.eval({x:X_test, y:y_test}))
33
---> 34 train_neural_net(X)
<ipython-input-16-8130c429fbca> in train_neural_net(x)
23 print(batch_y)
24
---> 25 _, c = sess.run([optimizer, cost], feed_dict= .
{x: batch_x, y: batch_y})
26 epoch_loss += c
27
/anaconda3/lib/python3.6/site-
packages/tensorflow/python/client/session.py in run(self, fetches,
feed_dict, options, run_metadata)
903 try:
904 result = self._run(None, fetches, feed_dict, options_ptr,
--> 905 run_metadata_ptr)
906 if run_metadata:
907 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/anaconda3/lib/python3.6/site-
packages/tensorflow/python/client/session.py in _run(self, handle,
fetches, feed_dict, options, run_metadata)
1104 feed_handles[subfeed_t] = subfeed_val
1105 else:
-> 1106 np_val = np.asarray(subfeed_val,
dtype=subfeed_dtype)
1107
1108 if (not is_tensor_handle_feed and
/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py in
asarray(a, dtype, order)
490
491 """
--> 492 return array(a, dtype, copy=False, order=order)
493
494
ValueError: setting an array element with a sequence.
修改
建议我使用.toarray()
将备用矩阵转换为数组后,我收到错误ValueError: Cannot feed value of shape (128, 9703) for Tensor 'Placeholder_2:0', which has shape '(?, 6612)'
我研究过并认为它与here不同,因为我有不同PlaceHolder中的值,同时链接具有不同的维度。还不确定要去哪里,谢谢!
任何想法?提前致谢