Tensorflow,assign value to variable - tensorflow

I am total newbie to tensorflow, I am learning from
https://www.tensorflow.org/get_started/get_started
fixW = tf.assign(W, [-1.])
works fine,but
fixb = tf.assign(b, [1.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'
One other example
zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>
If I try to change zero_tsr
fixz = tf.assign(zero_tsr, [2.,2.])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/milenko/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 272, in assign
return ref.assign(value)
AttributeError: 'Tensor' object has no attribute 'assign'
Again,the same problem.
I have not changed shell,everything is the same.Why do I have problem here?

In the example you posted:
zero_tsr = tf.zeros([1.,2.])
zero_tsr
<tf.Tensor 'zeros:0' shape=(1, 2) dtype=float32>
zero_tsr is a constant and not a variable, so you cannot assign a value to it.
From the documentation:
assign(
ref,
value,
validate_shape=None,
use_locking=None,
name=None )
ref: A mutable Tensor. Should be from a Variable node. May be
uninitialized.
For example, this will work fine:
import tensorflow as tf
zero_tsr = tf.Variable([0,0])
tf.assign(zero_tsr,[4,5])
while this code will raise an error
import tensorflow as tf
zero_tsr = tf.zeros([1,2])
tf.assign(zero_tsr,[4,5])
The error that is raised is exactly the error you posted:
AttributeError: 'Tensor' object has no attribute 'assign'

Related

tensorlayer can't load pretrained weight propaly

I have a problem to load pretrained params in tensorlayer.
I try to use srgan (https://github.com/tensorlayer/srgan) and its pretrained params (https://github.com/tensorlayer/srgan/releases/tag/1.2.0)
Applicable code is below
G = get_G([1, None, None, 3])
load_params = tl.files.load_npz(path='', name='g_srgan.npz')
tl.files.assign_weights(load_params,
#G.load_weights(os.path.join("g_srgan.npz"))
G.eval()
I got error:
Traceback (most recent call last):
File "train.py", line 194, in <module>
evaluate(session)
File "train.py", line 156, in evaluate
tl.files.assign_weights(load_params, G)
File "/usr/local/lib/python3.6/dist-packages/tensorlayer/files/utils.py", line 2023, in assign_weights
ops.append(network.all_weights[idx].assign(param))
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py", line 819, in assign
self._shape.assert_is_compatible_with(value_tensor.shape)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/tensor_shape.py", line 1110, in assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (1, 1, 1, 64) and (64,) are incompatible
Please tell me the solution.
Thanks !!

Tensorflow Recognize Flowers using transfer learning error generator

I tried to follow this popular Tensorflow tutorial.
Which basically should be a copy-paste, but I still get an error in my code.
I have tried use None as a shape but that does not work. And since I am very new at this I am not really sure what to google for.
This is the code where the error occurs;
epochs = 10
history = model.fit(train_generator,
epochs=epochs,
validation_data=val_generator)
I get this error message:
Traceback (most recent call last):
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: ValueError: `generator` yielded an element of shape (59, 224, 224, 3) where an element of shape (64, 224, 224, 3) was expected.
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/script_ops.py", line 221, in __call__
ret = func(*args)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 621, in generator_py_func
"of shape %s was expected." % (ret_array.shape, expected_shape))

ValueError: ('Could not interpret initializer identifier:', 0.2)

Traceback (most recent call last): File
"AutoFC_AlexNet_randomsearch_CalTech101_v2.py", line 112, in
X = layers.Dense(neurons, activation=activation, kernel_initializer=weight_init)(X) File
"/home/shabbeer/NAS/lib/python3.5/site-packages/keras/legacy/interfaces.py",
line 91, in wrapper
return func(*args, **kwargs) File "/home/shabbeer/NAS/lib/python3.5/site-packages/keras/layers/core.py",
line 824, in init
self.kernel_initializer = initializers.get(kernel_initializer) File
"/home/shabbeer/NAS/lib/python3.5/site-packages/keras/initializers.py",
line 503, in get
identifier) ValueError: ('Could not interpret initializer identifier:', 0.2)
I am getting the above error when running the code using tensorflow-gpu version 1.4.0 and keras version 2.1.3
you should change it to X = layers.Dense(neurons, activation=activation, kernel_initializer=keras.initializers.Constant(weight_init))(X)

tensorflow keras conv1d : ValueError: len(dilation_rate)=1 but should be -1

I try to build a custom Keras regularize with tensorflow as backend.
Executing the following piece of code gives me an exception :
import tensorflow as tf
from tensorflow import keras
inputs = keras.Input(shape=(10,))
x = keras.backend.conv1d(inputs, tf.constant([-1,1]), padding = 'same', dilation_rate=None)
x = keras.backend.conv1d(inputs, tf.constant([-1,1]), padding = 'same', dilation_rate=None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/keras/backend.py", line 3775, in conv1d
data_format=tf_data_format)
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 779, in convolution
data_format=data_format)
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 842, in __init__
num_spatial_dims, strides, dilation_rate)
File "/home/arthur/miniconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/nn_ops.py", line 625, in _get_strides_and_dilation_rate
(len(dilation_rate), num_spatial_dims))
ValueError: len(dilation_rate)=1 but should be -1
i can't understand what i am doing wrong.
Thank you.
I think the problem is in tf.constant([-1,1]). This is a place for kernel which should have dimension like input_length,in_channel,out_channel.

Compute status: Not found: Tensor name "input_producer/limit_epochs/epochs" not found in checkpoint files

I'm using the CIFAR10 example. I trained the net as it is with the code provided. The training was done successfully. As I wanted to evaluate each example only once on my data set, I have modified inputs in cifar10_input.py to the following.
def inputs(eval_data, data_dir, batch_size):
filename = os.path.join(data_dir, TEST_FILE)
filename_queue = tf.train.string_input_producer([filename],num_epochs=1)
image, label = read_and_decode(filename_queue)
float_image = tf.image.per_image_whitening(image)
min_fraction_of_examples_in_queue = 0.4
min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_EVAL *
min_fraction_of_examples_in_queue)
images, label_batch = tf.train.batch(
[image, label],
batch_size=batch_size,
num_threads=1,
capacity=min_queue_examples + 3 * batch_size)
tf.image_summary('images', images)
return images, tf.reshape(label_batch, [batch_size])
I have isolated the problem to the following:
tf.train_string_input_producer([filename], num_epochs = 1)
If I don't set num_epochs = 1, everything works fine as it is. If I do, I get the following error.
0x2cf2700 Compute status: Not found: Tensor name "input_producer/limit_epochs/epochs" not found in checkpoint files /home/jkschin/tensorflow/my_code/data/svhn/train/model.ckpt-8000
Thank you for your help!
EDIT 3 #mrry:
It still fails. Here's the trace.
Traceback (most recent call last):
File "cnn_eval.py", line 148, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/default/_app.py", line 30, in run
sys.exit(main(sys.argv))
File "cnn_eval.py", line 144, in main
evaluate()
File "cnn_eval.py", line 119, in evaluate
saver = tf.train.Saver([v for v in variables_to_restore if v.name != "input_producer/limit_epochs/epochs"])
AttributeError: 'unicode' object has no attribute 'name'
EDIT 4 #mrry:
softmax_linear/biases/ExponentialMovingAverage
conv2/biases/ExponentialMovingAverage
local4/biases/ExponentialMovingAverage
local3/biases/ExponentialMovingAverage
softmax_linear/weights/ExponentialMovingAverage
conv1/biases/ExponentialMovingAverage
local4/weights/ExponentialMovingAverage
conv2/weights/ExponentialMovingAverage
input_producer/limit_epochs/epochs
local3/weights/ExponentialMovingAverage
conv1/weights/ExponentialMovingAverage
Traceback (most recent call last):
File "cnn_eval.py", line 148, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/default/_app.py", line 30, in run
sys.exit(main(sys.argv))
File "cnn_eval.py", line 144, in main
evaluate()
File "cnn_eval.py", line 119, in evaluate
saver = tf.train.Saver([v for v in variables_to_restore if v != "input_producer/limit_epochs/epochs"])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 784, in __init__
restore_sequentially=restore_sequentially)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 437, in build
vars_to_save = self._ValidateAndSliceInputs(names_to_variables)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 340, in _ValidateAndSliceInputs
names_to_variables = self._VarListToDict(names_to_variables)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 314, in _VarListToDict
raise TypeError("Variable to save is not a Variable: %s" % var)
TypeError: Variable to save is not a Variable: Tensor("Const:0", shape=(), dtype=string)
EDIT 5 #mrry:
saver = tf.train.Saver([tf.Variable(0.0,validate_shape=False,name=v) for v in variables_to_restore if v != "input_producer/limit_epochs/epochs"])
0x21d0cb0 Compute status: Invalid argument: Assign requires shapes of both tensors to match. lhs shape= [] rhs shape= [10]
[[Node: save/Assign_8 = Assign[T=DT_FLOAT, use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](softmax_linear/biases/ExponentialMovingAverage, save/restore_slice_8/_20)]]
TL;DR: In cifar10_eval.py, change the saver constructor so that it is:
saver = tf.train.Saver([v for v in variables_to_restore
if v != "input_producer/limit_epochs/epochs"])
This problem arises because tf.train.string_input_producer() internally creates a variable (called "input_producer/limit_epochs/epochs") when its num_epochs argument is not None. When, in cifar10_eval.py a tf.train.Saver is created, it uses tf.all_variables(), which includes the implicitly-created variable from the tf.nn.string_input_producer(). This list of variables determines the set of names that TensorFlow looks up in the checkpoint file.
Currently there isn't a great way to refer to implicitly created variables, other than by their name. Therefore, the best fix is to exclude the variable from the Saver constructor by name.
Another way of eliminating the implicit variable "input_producer/limit_epochs/epochs" is to only load the trainable variables:
saver = tf.train.Saver(tf.trainable_variables())