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.
python-3.x tensorflow machine-learning
add a comment |
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.
python-3.x tensorflow machine-learning
add a comment |
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.
python-3.x tensorflow machine-learning
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
python-3.x tensorflow machine-learning
asked Nov 11 at 7:15
Nacho
18213
18213
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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