import tensorflow as tf
import random
import numpy as np
x = tf.placeholder('float')
x = tf.reshape(x, [-1,28,28,1])
with tf.Session() as sess:
x1 = np.asarray([random.uniform(0,1) for i in range(784)])
result = sess.run(x, feed_dict={x: x1})
print(result)
I had some problems using mnist data on reshaping, but this question is simplified version of my problem...
Why actually isn't this code working?
It shows
"ValueError: Cannot feed value of shape (784,) for Tensor 'Reshape:0', which has shape '(?, 28, 28, 1)' ".
How could I solve it?
After you reassign, x is a tensor with shape [-1,28,28,1] and as error says, you cannot shape (784,) to (?, 28, 28, 1). You can use a different variable name:
import tensorflow as tf
import random
import numpy as np
x = tf.placeholder('float')
y = tf.reshape(x, [-1,28,28,1])
with tf.Session() as sess:
x1 = np.asarray([random.uniform(0,1) for i in range(784)])
result = sess.run(y, feed_dict={x: x1})
print(result)
Conceptually
You get error here because when you are using sess.run(x, feed_dict{x:x1}). This is trying to feed and reshape same variable. This creates a problem in run time. Thus you cannot do this using a single variable.
import tensorflow as tf
import random
import numpy as np
x = tf.placeholder('float')
y = tf.reshape(x, [-1,28,28,1])
with tf.Session() as sess:
x1 = np.asarray([random.uniform(0,1) for i in range(784)])
result = sess.run(y, feed_dict={x: x1})
print(result)
In tensorflow, variables are place holders. So, x will hold floating point values, and another variable say y will hold values in shape [-1,28,28,1].
If same variable name is used then it has to act as placeholder for two things. This is not possible.
Related
I am trying to create a Neural Network with Tensorflow and I am trying to use a pandas dataframe as my data. This gives me an error saying that I cannot convert a dataframe into a Tensor. I thought that passing the dataframe through numpy.asarray() should have fixed this error but I still get the error.
This is my code:
import numpy as np
import tensorflow as tf
import pandas as pd
dataframe = pd.read_csv('data.csv')
dataframe.drop(dataframe.columns.difference(["Happiness.Score", "Freedom", "Family", "Generosity"]), 1, inplace=True)
train = dataframe[1:11]
test = dataframe[12:22]
test.pop("Happiness.Score")
dataY = np.asarray(train["Happiness.Score"])
dataX = np.asarray(train.drop(["Happiness.Score"], axis=1))
inputX = tf.placeholder(tf.float32, [10, 3])
inputY = tf.placeholder(tf.float32, [10])
W = tf.Variable(tf.zeros([3, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(inputX, W) + b)
cross_entropy = tf.reduce_sum(y * tf.log(inputY))
optimizer = tf.train.GradientDescentOptimizer(.01)
trainer = optimizer.minimize(cross_entropy)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(1000):
sess.run(train, feed_dict={inputX: dataX, inputY: dataY})
print(sess.run(cross_entropy, feed_dict={inputX: dataX, inputY: dataY}))
sess.close()
This throws the error
has invalid type , must be a string or Tensor. (Can not convert a DataFrame into a Tensor or Operation.)
Any ideas on how to fix this?
Did you mean to use this line ?
sess.run(trainer, feed_dict={inputX: dataX, inputY: dataY})
You are using this line now.
sess.run(train, feed_dict={inputX: dataX, inputY: dataY})
I'm attempting to filter a matrix that represents a point cloud in tensorflow. It is an n x 3 matrix.
I only want to keep rows with z > eps. This corresponds to column index 2 of the matrix.
I have the following code:
import numpy as np
import tensorflow as tf
point_cloud = tf.placeholder(tf.float32, shape=[None,3])
eps = tf.placeholder(tf.float32)
mask = tf.greater(point_cloud[:,2], eps)
reduced_cloud = tf.boolean_mask(point_cloud, mask)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
_cloud = np.random.rand(5000,3)
feed = {point_cloud:_cloud, eps:0.0025}
_filtered = sess.run(reduced_cloud, feed_dict=feed)
When I run the above code I get this:
ValueError: Number of mask dimensions must be specified, even if some dimensions are None. E.g. shape=[None] is ok, but shape=None is not.
I don't understand the error message, having tried to specify shape in a number of places with no success, and the documentation seems to suggest the boolean_mask only works with np.arrays. Is there any way to do this entirely on the tensorflow graph?
You haven't specified the shape of eps which needs to be a 1D-tensor:
import numpy as np
import tensorflow as tf
point_cloud = tf.placeholder(tf.float32, shape=[None,3])
eps = tf.placeholder(tf.float32, shape=[None])
mask = tf.greater(point_cloud[:,2], eps)
reduced_cloud = tf.boolean_mask(point_cloud, mask)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
_cloud = np.random.rand(5000,3)
feed = {point_cloud:_cloud, eps:[0.0025]}
_filtered = sess.run(reduced_cloud, feed_dict=feed)
I am new to tensorflow. This code is just for a simple neural network.
I think the problem maybe is from:
x_data = np.linspace(-0.5,0.5,200)[:np..newaxis]
I tried to write without [:np.newaxis], but it looks like the same.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_data = np.linspace(-0.5,0.5,200)[:np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
Weights_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
Weights_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2)
loss = tf.reduce_mean(tf.square(y-prediction))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
prediction_value = sess.run(prediction,feed_dict={x:x_data})
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,prediction_value,'r-',lw=5)
plt.show()
The defined placeholders (both x and y) are 2-dimensional, so you should reshape the input arrays to rank 2. Try to add this:
x_data = x_data.reshape([-1,1])
y_data = y_data.reshape([-1,1])
I am new to Tensorflow. I am trying to write a function in python using Tensorflow that operates on a sparse matrix input. Normally I would define a tensorflow placeholder, but apparently there is no placeholder for sparse matrices.
What is the proper way to define a function that operates on sparse data in tensorflow and pass values into it?
Specifically, I am trying to rewrite the fundamental example of a multilayer perceptron, found here https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py, to accept sparse input instead of dense.
As a dummy example, how would you write a function that looks something like this?
import tensorflow as tf
x = tf.placeholder("sparse")
y = tf.placeholder("float", [None, n_classes])
# Create model
def sparse_multiply(x, y):
outlayer = tf.sparse_tensor_dense_matmul(x, y)
return out_layer
pred = multiply(x, y)
# Launch the graph
with tf.Session() as sess:
result = sess.run(pred, feed_dict={x: x_input, y: y_input})
Someone at the link https://github.com/tensorflow/tensorflow/issues/342 recommended, as a workaround, passing in the elements needed to construct the sparse matrix and then creating the sparse matrix on the fly within the function. That seems a little hacky, and I get errors when I try to construct it that way.
Any help, especially answers with code, would be greatly appreciated!
I think I figured it out. The suggestion I linked to actually did work, I just needed to correct all the inputs to have consistent types. Here is the dummy example I listed in the question, coded correctly:
import tensorflow as tf
import sklearn.feature_extraction
import numpy as np
def convert_csr_to_sparse_tensor_inputs(X):
coo = X.tocoo()
indices = np.mat([coo.row, coo.col]).transpose()
return indices, coo.data, coo.shape
X = ____ #Some sparse 2 x 8 csr matrix
y_input = np.asarray([1, 1, 1, 1, 1, 1, 1, 1])
y_input.shape = (8,1)
x_indices, x_values, x_shape = convert_csr_to_sparse_tensor_inputs(X)
# tf Graph input
y = tf.placeholder(tf.float64)
values = tf.placeholder(tf.float64)
indices = tf.placeholder(tf.int64)
shape = tf.placeholder(tf.int64)
# Create model
def multiply(values, indices, shape, y):
x_tensor = tf.SparseTensor(indices, values, shape)
out_layer = tf.sparse_tensor_dense_matmul(x_tensor, y)
return out_layer
pred = multiply(values, indices, shape, y)
# Launch the graph
with tf.Session() as sess:
result = sess.run(pred, feed_dict={values: x_values, indices: x_indices, shape: x_shape, y: y_input})
I'm following this tutorial:
https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html#mnist-for-ml-beginners
What I want to be able to do is pass in a test image x - as a numpy array, and see the resulting softmax classification values - perhaps as another numpy array. Everything I can find online about testing tensor flow models works by passing in test values and test labels and the outputting the accuracy. In my case, I want to output the model labels just based on the test values.
This is what Im trying:
import tensorflow as tf
import numpy as np
from skimage import color,io
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#so now its trained successfully, and W and b should be the stored "model"
#now to load in a test image
greyscale_test = color.rgb2gray(io.imread('4.jpeg'))
greyscale_expanded = np.expand_dims(greyscale_test,axis=0) #now shape (1,28,28)
x = np.reshape(greyscale_expanded,(1,784)) #now same dimensions as mnist.train.images
#initialize the variable
init_op = tf.initialize_all_variables()
#run the graph
with tf.Session() as sess:
sess.run(init_op) #execute init_op
print (sess.run(feed_dict={x:x})) #this is pretty much just a shot in the dark. What would go here?
Right now it results in this:
TypeError Traceback (most recent call last)
<ipython-input-116-f232a17507fb> in <module>()
36 sess.run(init_op) #execute init_op
---> 37 print (sess.run(feed_dict={x:x})) #this is pretty much just a shot in the dark. What would go here?
TypeError: unhashable type: 'numpy.ndarray'
So when training, the sess.run is passed a train_step and a feed_dict. When I am trying to evaluate a tensor x, would this go in the feed dict? Would I even use sess.run?(seems I have to), but what would the train_step be? Is there a "test_step" or "evaluate_step"?
You're getting the TypeError because you are using a (mutable) numpy.ndarray as a key for your dictionary but the key should be a tf.placeholder and the value a numpy array.
The following adjustment fixes this problem:
x_placeholder = tf.placeholder(tf.float32, [None, 784])
# ...
x = np.reshape(greyscale_expanded,(1,784))
# ...
print(sess.run([inference_step], feed_dict={x_placeholder:x}))
If you just want to perform inference on your model, this will print a numpy array with the predictions.
If you want to evaluate your model (for example compute the accuracy) you also need to feed in the corresponding ground truth labels y as in:
accuracy = sess.run([accuracy_op], feed_dict={x_placeholder:x, y_placeholder:y}
In your case, the accuracy_op could be defined as follows:
correct_predictions = tf.equal(tf.argmax(predictions, 1), tf.cast(labels, tf.int64))
accuracy_op = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
Here, predictions is the output tensor of your model.
your tf.Session.run op needs a fetches
tf.Session.run(fetches, feed_dict=None, options=None, run_metadata=None)
https://www.tensorflow.org/versions/r0.9/api_docs/python/client.html#session-management
print (sess.run(train_step,feed_dict={x:x})) #but it also needs a feed_dict for y_
what do you mean with:
print the random values that we sample