0

Hello and let me start with by saying my experience with StackOverflow is extremely limited likewise with my experience programming. Needless to say, if I make any unintentional mistakes in the format of StackOverflow or the format of my code, I apologize and am welcome to edit my post.

I've done extensive research so to not create a duplicate and found posts like this one but no matter how hard I try, I either cant derive meaning from the post, or have too little knowledge in how to implement it

So here's my problem:

I'm making similar to a sentiment analysis machine learning algorithm, and I use Sklearns CountVectorizer and Tfidf on about 6,000 tweets, which turned into the following <6612x9703 sparse matrix of type '<class 'numpy.float64'> with 47719 stored elements in Compressed Sparse Row format>

From there I try to train on it using the following function:

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)

However when I run I get the error:

    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.

Edit:

After a recommendation that I turn the Spare Matrix into an array using .toarray() , I receive the error ValueError: Cannot feed value of shape (128, 9703) for Tensor 'Placeholder_2:0', which has shape '(?, 6612)' I researched and believe it is not the same as here as I have different values in the PlaceHolder, meanwhile the link had different Dimensions. Still unsure on where to go, thanks!

Any Ideas? Thanks in advance

Leo C
  • 11
  • 4
  • Try converting sparse to dense: `X_train_spmx = X_train_spmx.toarray()`. – Vivek Kumar Apr 30 '18 at 06:42
  • @VivekKumar i get this error `ValueError: Cannot feed value of shape (128, 9703) for Tensor 'Placeholder_2:0', which has shape '(?, 6612)' ` – Leo C Apr 30 '18 at 17:22

0 Answers0