train simple linear model with sparse input in tensorflow











up vote
0
down vote

favorite












I'm trying to train a simple linear model feeding it with a simulated sparse input matrix. I've no errors but the model does not learn. My first debug was to print the logits, which said me that I'm doing badly the operations, so I have an output matrix instead of a vector (I suppose that I'm performing outer product, probably I wrongly defined shapes):



import tensorflow as tf
import numpy as np
import pandas as pd
from scipy.sparse import coo_matrix
from sklearn.datasets import make_blobs
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split

samples = 800
# getting datasets
X_values, y_flat = make_blobs(n_features=2, n_samples=samples, centers=3, random_state=500)
y = OneHotEncoder().fit_transform(y_flat.reshape(-1, 1)).todense()
y = np.array(y)
X_train, X_test, y_train, y_test, y_train_flat, y_test_flat = train_test_split(X_values, y, y_flat)

X_test += np.random.randn(*X_test.shape) * 1.5

n_features = X_values.shape[1]
n_classes = len(set(y_flat))

weights_shape = (n_features, n_classes)
bias_shape = (1, n_classes)

b = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(bias_shape))
W = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(weights_shape))
x = tf.sparse.placeholder(tf.float32)
Y_true = tf.placeholder(dtype=tf.float32)

Y_pred = tf.sparse.matmul(x, W) + b

loss_function = tf.losses.softmax_cross_entropy(Y_true, Y_pred)
learner = tf.train.GradientDescentOptimizer(0.1).minimize(loss_function)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
indices = np.vstack([coo_matrix(X_train).row, coo_matrix(X_train).col]).T
values = coo_matrix(X_train).data
shape = np.array(coo_matrix(X_train).shape)

for i in range(100):
result = sess.run([learner, Y_pred], feed_dict={
x: tf.SparseTensorValue(indices, values, shape), Y_true: y_train}) # Will succeed.
if i % 10 == 0:
print(result)


I've followed the TF documentation for sparse matrix multiplication https://www.tensorflow.org/api_docs/python/tf/sparse/matmul, but simply I cant solve the problem.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to train a simple linear model feeding it with a simulated sparse input matrix. I've no errors but the model does not learn. My first debug was to print the logits, which said me that I'm doing badly the operations, so I have an output matrix instead of a vector (I suppose that I'm performing outer product, probably I wrongly defined shapes):



    import tensorflow as tf
    import numpy as np
    import pandas as pd
    from scipy.sparse import coo_matrix
    from sklearn.datasets import make_blobs
    from sklearn.preprocessing import OneHotEncoder
    from sklearn.model_selection import train_test_split

    samples = 800
    # getting datasets
    X_values, y_flat = make_blobs(n_features=2, n_samples=samples, centers=3, random_state=500)
    y = OneHotEncoder().fit_transform(y_flat.reshape(-1, 1)).todense()
    y = np.array(y)
    X_train, X_test, y_train, y_test, y_train_flat, y_test_flat = train_test_split(X_values, y, y_flat)

    X_test += np.random.randn(*X_test.shape) * 1.5

    n_features = X_values.shape[1]
    n_classes = len(set(y_flat))

    weights_shape = (n_features, n_classes)
    bias_shape = (1, n_classes)

    b = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(bias_shape))
    W = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(weights_shape))
    x = tf.sparse.placeholder(tf.float32)
    Y_true = tf.placeholder(dtype=tf.float32)

    Y_pred = tf.sparse.matmul(x, W) + b

    loss_function = tf.losses.softmax_cross_entropy(Y_true, Y_pred)
    learner = tf.train.GradientDescentOptimizer(0.1).minimize(loss_function)

    with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    indices = np.vstack([coo_matrix(X_train).row, coo_matrix(X_train).col]).T
    values = coo_matrix(X_train).data
    shape = np.array(coo_matrix(X_train).shape)

    for i in range(100):
    result = sess.run([learner, Y_pred], feed_dict={
    x: tf.SparseTensorValue(indices, values, shape), Y_true: y_train}) # Will succeed.
    if i % 10 == 0:
    print(result)


    I've followed the TF documentation for sparse matrix multiplication https://www.tensorflow.org/api_docs/python/tf/sparse/matmul, but simply I cant solve the problem.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to train a simple linear model feeding it with a simulated sparse input matrix. I've no errors but the model does not learn. My first debug was to print the logits, which said me that I'm doing badly the operations, so I have an output matrix instead of a vector (I suppose that I'm performing outer product, probably I wrongly defined shapes):



      import tensorflow as tf
      import numpy as np
      import pandas as pd
      from scipy.sparse import coo_matrix
      from sklearn.datasets import make_blobs
      from sklearn.preprocessing import OneHotEncoder
      from sklearn.model_selection import train_test_split

      samples = 800
      # getting datasets
      X_values, y_flat = make_blobs(n_features=2, n_samples=samples, centers=3, random_state=500)
      y = OneHotEncoder().fit_transform(y_flat.reshape(-1, 1)).todense()
      y = np.array(y)
      X_train, X_test, y_train, y_test, y_train_flat, y_test_flat = train_test_split(X_values, y, y_flat)

      X_test += np.random.randn(*X_test.shape) * 1.5

      n_features = X_values.shape[1]
      n_classes = len(set(y_flat))

      weights_shape = (n_features, n_classes)
      bias_shape = (1, n_classes)

      b = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(bias_shape))
      W = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(weights_shape))
      x = tf.sparse.placeholder(tf.float32)
      Y_true = tf.placeholder(dtype=tf.float32)

      Y_pred = tf.sparse.matmul(x, W) + b

      loss_function = tf.losses.softmax_cross_entropy(Y_true, Y_pred)
      learner = tf.train.GradientDescentOptimizer(0.1).minimize(loss_function)

      with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      indices = np.vstack([coo_matrix(X_train).row, coo_matrix(X_train).col]).T
      values = coo_matrix(X_train).data
      shape = np.array(coo_matrix(X_train).shape)

      for i in range(100):
      result = sess.run([learner, Y_pred], feed_dict={
      x: tf.SparseTensorValue(indices, values, shape), Y_true: y_train}) # Will succeed.
      if i % 10 == 0:
      print(result)


      I've followed the TF documentation for sparse matrix multiplication https://www.tensorflow.org/api_docs/python/tf/sparse/matmul, but simply I cant solve the problem.










      share|improve this question













      I'm trying to train a simple linear model feeding it with a simulated sparse input matrix. I've no errors but the model does not learn. My first debug was to print the logits, which said me that I'm doing badly the operations, so I have an output matrix instead of a vector (I suppose that I'm performing outer product, probably I wrongly defined shapes):



      import tensorflow as tf
      import numpy as np
      import pandas as pd
      from scipy.sparse import coo_matrix
      from sklearn.datasets import make_blobs
      from sklearn.preprocessing import OneHotEncoder
      from sklearn.model_selection import train_test_split

      samples = 800
      # getting datasets
      X_values, y_flat = make_blobs(n_features=2, n_samples=samples, centers=3, random_state=500)
      y = OneHotEncoder().fit_transform(y_flat.reshape(-1, 1)).todense()
      y = np.array(y)
      X_train, X_test, y_train, y_test, y_train_flat, y_test_flat = train_test_split(X_values, y, y_flat)

      X_test += np.random.randn(*X_test.shape) * 1.5

      n_features = X_values.shape[1]
      n_classes = len(set(y_flat))

      weights_shape = (n_features, n_classes)
      bias_shape = (1, n_classes)

      b = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(bias_shape))
      W = tf.Variable(dtype=tf.float32, initial_value=tf.random_normal(weights_shape))
      x = tf.sparse.placeholder(tf.float32)
      Y_true = tf.placeholder(dtype=tf.float32)

      Y_pred = tf.sparse.matmul(x, W) + b

      loss_function = tf.losses.softmax_cross_entropy(Y_true, Y_pred)
      learner = tf.train.GradientDescentOptimizer(0.1).minimize(loss_function)

      with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      indices = np.vstack([coo_matrix(X_train).row, coo_matrix(X_train).col]).T
      values = coo_matrix(X_train).data
      shape = np.array(coo_matrix(X_train).shape)

      for i in range(100):
      result = sess.run([learner, Y_pred], feed_dict={
      x: tf.SparseTensorValue(indices, values, shape), Y_true: y_train}) # Will succeed.
      if i % 10 == 0:
      print(result)


      I've followed the TF documentation for sparse matrix multiplication https://www.tensorflow.org/api_docs/python/tf/sparse/matmul, but simply I cant solve the problem.







      python-3.x tensorflow machine-learning






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 7:15









      Nacho

      18213




      18213





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246616%2ftrain-simple-linear-model-with-sparse-input-in-tensorflow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246616%2ftrain-simple-linear-model-with-sparse-input-in-tensorflow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)