how to send values to function using feed_dict in tensorflow? - tensorflow

I'm beginner in tensorflow and i'm working on a project which i want to send values to a placeholder and use this placeholder in a function so i will simplify what i want.
This is a simple code
import tensorflow as tf
import glob
from PIL import Image
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
x = tf.placeholder(dtype=tf.float32,shape=[1])
def fun():
print(x)
return x
with tf.Session() as sess:
sess.run(fun(),feed_dict={x:[5.]})
I want to use X value inside the function but when i print it i get the shape only however i used sess.run to run the function so that i expect to print the value not the shape but also when i use print(sess.run(x)) it give me error and say i must feed X with value so what am i missing ?

You should write it like that:
x = tf.placeholder(dtype=tf.float32,shape=[1])
def fun():
return x
with tf.Session() as sess:
y=sess.run(fun(),feed_dict={x:[5.]})
print(y)

Related

Why is GradientTape returning None when I use numpy math

Why is GradientTape returning None when I use numpy math
I am trying to understand tensorflow GradientTape calculation for RL loss function. When I call a function using np.math the GradientTape returns None. If I use tf.math in the function it works fine. I have looked at tf-agents like ppo and sac and they are doing exactly(?) what I am trying to do (I have tried at last 50 other versions).
What's wrong in the code below? What am I missing?
window 10, python 3.6.8, tensorflow 2.0.0
ref:https://github.com/chagmgang/tf2.0_reinforcement_learning/blob/master/policy/ppo.py
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
def my_loss1(x):
y=tf.sin(x)
y=tf.abs(y)
return y
def my_loss2(x):
y=np.sin(x)
y=np.abs(y)
return y
def main(ver):
x = np.linspace(0,10,25)
dsin_dx=np.cos(x)
xx = tf.constant(x)
with tf.GradientTape() as tape:
tape.watch(xx)
if ver==0:
# my_loss1 with tf math
loss1=my_loss1(xx)
if ver==1:
#my loss with numpy math
loss1=my_loss2(np.array(xx))
loss1 = tf.convert_to_tensor(loss1, dtype=tf.float64)
print(loss1)
loss=tf.reduce_sum(loss1)
print('loss=',loss)
grads = tape.gradient(loss, xx)
fig, ax = plt.subplots(2)
ax[0].plot(x,loss1,'r')
print('grads', grads)
if not grads is None:
ax[1].plot(x, grads)
ax[1].plot(x,dsin_dx)
plt.show()
if __name__ == '__main__':
main(ver=0) # This works ok
main(ver=1) # This returns grads = None
The problem is that the Gradient tape only records tensors. Numpy variables are not recorded why the gradient can't be calqulated in case ver=1. Loss1 in ver1 looks identical to loss1 in ver=0 but the dependentsy to xx is broken by numpy.
My ref. has this error when calculation get_gaes() and the calculation of the grads is incorrect.

Problem in computing the gradient of a function

I want to differentiate a vector with respect to another using TensorFlow. I am unable to write and visualize the output (just started my journey on TensorFlow)
I am attaching the code snippet I have tried.
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 120)
y = np.sinh(x)
plt.plot(x,y)
plt.axhline(color="gray", zorder=-1)
plt.axvline(color="gray", zorder=-1)
plt.show()
X = tf.constant(x, dtype=tf.float32)
Y = tf.constant(y, dtype=tf.float32)
gradient = tf.gradients(Y, X)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
I am unable to output the gradient. I also tried a placeholder for the gradients but cannot figure out how to go about.
Your Y doesn't depend on X. The way you have defined them they are just two independent tensors. This is probably what you want:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
x_data = np.linspace(-np.pi, np.pi, 120)
y_data = np.sinh(x_data)
plt.plot(x_data, y_data)
plt.axhline(color="gray", zorder=-1)
plt.axvline(color="gray", zorder=-1)
plt.show() # <-- shows image
x = tf.constant(x_data, dtype=tf.float32)
y = tf.math.sinh(x) # <-- `y` is a function of `x`
grads = tf.gradients(y, x)
# init = tf.global_variables_initializer() # <-- No need, you don't have variables here
with tf.Session() as sess:
print(sess.run(grads)) # <-- prints long array

How can I print output (tensor values, shapes) in gpflow?

I am trying to develop a new model within gpflow. In order to debug it I need to know shapes and values of tensors during execution of the graph.
I tried the below based on printing tensor values in tensorflow, but nothing is printed to the console.
import numpy as np
import sys
import gpflow
from gpflow.mean_functions import MeanFunction
from gpflow.decors import params_as_tensors
class Log(MeanFunction):
"""
:math:`y_i = \log(x_i)`
"""
def __init__(self):
MeanFunction.__init__(self)
#params_as_tensors
def __call__(self, X):
# I want to figure out the shape of X here
tf.print(tf.shape(X), output_stream=sys.stdout)
# Returns the natural logarithm of the input
return tf.log(X)
# Test gpflow implementation
sess = tf.InteractiveSession()
with sess.as_default(), sess.graph.as_default():
X = np.random.uniform(size=[100, 1])
y = np.random.uniform(size=[100, 1])
m = gpflow.models.GPR(X=X, Y=y, mean_function=Log(), kern=gpflow.kernels.RBF(input_dim=1))
You're on the right track. According to the TensorFlow docs [1], you need to wrap tf.print() in a tf.control_dependencies() context manager to make sure it's run, when in graph model. GPflow currently works in graph model. GPflow 2.0, which is indevelopment, will allow usage in eager mode.
#params_as_tensors
def __call__(self, X):
# I want to figure out the shape of X here
print_op = tf.print(tf.shape(X), output_stream=sys.stdout)
with tf.control_dependencies([print_op]):
log_calc = tf.log(X)
# Returns the natural logarithm of the input
return log_calc
[1] https://www.tensorflow.org/api_docs/python/tf/print

TensorFlow returns the same value when using numpy random function

I am using Tensorflow with numpy random function, but the output is the same value. How can I generate different values? You may recommend use native tf random functions, but I need to use numpy random function.
import tensorflow as tf
import random
def get_rand():
return random.randint(0,5)
a = get_rand()
tfprint = tf.Print(a, [a])
for i in range(10):
print(print(get_rand()))
with tf.Session() as sess:
for i in range(10):
sess.run(tfprint)
With tf.py_func, turn the Numpy function to Tensorflow function.
import tensorflow as tf
import random
def get_rand():
return random.randint(0,5)
a = tf.py_func(get_rand, [], tf.int64)
tfprint = tf.Print(a, [a])
for i in range(10):
print(get_rand())
with tf.Session() as sess:
for i in range(10):
sess.run(tfprint)
You need to feed data using placeholders and the feed_dict variable:
import tensorflow as tf
import random
def get_rand():
return random.randint(0,5)
a = tf.placeholder(tf.int32)
tfprint = tf.Print(a, [a])
with tf.Session() as sess:
for i in range(10):
sess.run(tfprint, feed_dict={a: get_rand()})
You may read more about placeholders here

'DataFrame' object has no attribute 'train'

Please help me where is my missing? why I always get this error:
'DataFrame' object has no attribute 'train'
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.contrib import rnn
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv("all.csv")
x = dataset.iloc[:, 1:51].values
y = dataset.iloc[:, 51].values
time_steps=5
num_units=128
n_input=50
learning_rate=0.001
n_classes=2
batch_size=5
#weights and biases of appropriate shape to accomplish above task
out_weights=tf.Variable(tf.random_normal([num_units,n_classes]))
out_bias=tf.Variable(tf.random_normal([n_classes]))
#defining placeholders
#input image placeholder
x=tf.placeholder("float",[None,time_steps,n_input])
#input label placeholder
y=tf.placeholder("float",[None,n_classes])
#processing the input tensor from [batch_size,n_steps,n_input] to
"time_steps"
number of [batch_size,n_input] tensors
input=tf.unstack(x ,time_steps,1)
#defining the network
lstm_layer=rnn.BasicLSTMCell(num_units,forget_bias=1)
outputs,_=rnn.static_rnn(lstm_layer,input,dtype="float32")
#converting last output of dimension [batch_size,num_units] to
[batch_size,n_classes] by out_weight multiplication
prediction=tf.matmul(outputs[-1],out_weights)+out_bias
#loss_function
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
#optimization
opt=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
#model evaluation
correct_prediction=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#initialize variables
init=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
iter=1
while iter<800:
batch_x,batch_y=dataset.train.next_batch(batch_size=batch_size)
batch_x=batch_x.reshape((batch_size,time_steps,n_input))
sess.run(opt, feed_dict={x: batch_x, y: batch_y})
if iter %10==0:
acc=sess.run(accuracy,feed_dict={x:batch_x,y:batch_y})
los=sess.run(loss,feed_dict={x:batch_x,y:batch_y})
print("For iter ",iter)
print("Accuracy ",acc)
print("Loss ",los)
print("__________________")
iter=iter+1
As the error states, your pandas "DataFrame" object has no attribute/method called "next_batch".
You probably followed a tutorial that used Tensorflow helper methods to load the MNIST database maybe. But pandas return a different object than the "DataSet" class you are expecting.