使用Conv1D进行TensorFlow重塑

时间:2017-12-24 11:36:53

标签: python-3.x tensorflow

我在Stack Overflow上看到类似于我的问题,但不完全一样。我可以在使用完全连接的NN层时重塑,但不能使用Conv1D层。这是一个很小的例子。我在Python 3.6.3上使用TF 1.4.0。

import tensorflow as tf

# fully connected
fc = tf.placeholder(tf.float32, [None,12])
fc = tf.contrib.layers.fully_connected(fc, 12)
fc = tf.contrib.layers.fully_connected(fc, 6)
fc = tf.reshape(fc, [-1,3,2])

# convolutional
con = tf.placeholder(tf.float32, [None,50,4])
con = tf.layers.Conv1D(con, 12, 3, activation=tf.nn.relu)
con = tf.layers.Conv1D(con, 6, 3, activation=tf.nn.relu)
con = tf.reshape(con, [-1,50,3,2])

这是输出(是的,我知道RuntimeWarning。我发现的消息,它讨论它表明它是无害的,但如果你不知道,请分享!):

/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 468, in make_tensor_proto
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 468, in <listcomp>
    str_values = [compat.as_bytes(x) for x in proto_values]
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
    (bytes_or_text,))
TypeError: Expected binary or unicode string, got <tensorflow.python.layers.convolutional.Conv1D object at 0x7fa67e0d1a20>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "minimal reshape example.py", line 16, in <module>
    con = tf.reshape(con, [-1,width,3,2])
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3938, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 513, in _apply_op_helper
    raise err
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 472, in make_tensor_proto
    "supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'tensorflow.python.layers.convolutional.Conv1D'> to Tensor. Contents: <tensorflow.python.layers.convolutional.Conv1D object at 0x7fa67e0d1a20>. Consider casting elements to a supported type.

我的代码在con = tf.reshape(con,[ - 1,5,3,2])处失败。然而,该模式几乎与我用于完全连接图fc。

的模式相同

我在名为TFLearn的TensorFlow的高级API中使网络与这些工作非常相似。但是,TFLearn's DNN Estimator object is having trouble managing a tf.Session correctly。一个多月后,我还没有{GttHub上的TFLearn开发人员resolve the issue

我不介意使用TensorFlow的原生Estimator,但我必须解决这个重塑问题才能实现它。

1 个答案:

答案 0 :(得分:1)

好吧,我发现了错误: tf.layers.Conv1D!= tf.layers.conv1d 。将前者改为后者消除了错误。让TensorFlow / Python用户小心!

尽管TensorFlow似乎避免使用Python的对象模型(考虑到分布式,低级计算的可能性,这可能是必要的),但事实上Python API中有一些真正的类。类构造函数可以接受许多(所有?)与类似命名的便捷函数相同的参数。