Tflearn: Constant weights initialization?

Created on 27 Jul 2016  路  4Comments  路  Source: tflearn/tflearn

Hi,

I'm trying to create a conv2d layer with constant weights as follows:

conv_2d(net, num_filters, 3, strides=1,weights_init=weights, bias_init=biases, trainable=False, activation='relu',name=layer)

When I do this I receive the error message: If initializer is a constant, do not specify shape.

However, none of the optional parameters is a shape. How do I proceed here?

Most helpful comment

You can try tf.constant_initializer(weights) instead

All 4 comments

You can try tf.constant_initializer(weights) instead

Thanks! Also, what shape should the weights be? Right now I have them as [height, width, in_channels, out_channels] - is this correct? I'm now receiving the error

Traceback (most recent call last):
  File "/home/logan/test/nets.py", line 56, in vgg
    trainable=False, activation='relu', name=layer)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tflearn/layers/conv.py", line 78, in conv_2d
    trainable=trainable, restore=restore)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tflearn/variables.py", line 54, in variable
    collections=collections)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 732, in get_variable
    partitioner=partitioner, validate_shape=validate_shape)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 596, in get_variable
    partitioner=partitioner, validate_shape=validate_shape)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 161, in get_variable
    caching_device=caching_device, validate_shape=validate_shape)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 478, in _get_single_variable
    validate_shape=validate_shape)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 211, in __init__
    dtype=dtype)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 277, in _init_from_args
    self._initial_value = ops.convert_to_tensor(initial_value(),
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 468, in <lambda>
    init_val = lambda: initializer(shape.as_list(), dtype=dtype)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/init_ops.py", line 73, in _initializer
    return constant_op.constant(value, dtype=dtype, shape=shape)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.py", line 162, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 353, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "/home/logan/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 290, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected float32, got <tf.Tensor 'Const_6:0' shape=(3, 3, 3, 64) dtype=float32> of type 'Tensor' instead.

The shape is correct. What version of TFLearn are you using? Can you try to update it to the latest version?

pip install --upgrade git+https://github.com/tflearn/tflearn.git

I ended up just using vanilla TensorFlow for this. According to pip show I am using v0.2.1. I will roll back and try the latest version later, don't have time right now sorry!

Was this page helpful?
0 / 5 - 0 ratings