python - lstm tf.float64 tf.float32 之间存在转换问题

标签 python tensorflow types lstm

我尝试使用lstm进行情感分析。 tf版本是1.14.0。 我应用了标记化,并使用了嵌入矩阵手套。对于定义最后一个隐藏状态时的以下代码段,我会因此错误而停止。

import tensorflow as tf
batchSize = 64
numClasses = 2
maxlen = 100
embedding_dim = 50
lstmUnits = 64

tf.reset_default_graph()

labels = tf.placeholder(tf.float32, [batchSize, numClasses])
input_data = tf.placeholder(tf.int32, [batchSize, maxlen])

data = tf.Variable(tf.zeros([batchSize, maxlen, 
                   embedding_dim]),dtype=tf.float32)
data = tf.nn.embedding_lookup(embedding_matrix_glove,input_data)

lstmCell = tf.contrib.rnn.BasicLSTMCell(lstmUnits)
lstmCell = tf.contrib.rnn.DropoutWrapper(cell=lstmCell, 
                  output_keep_prob=0.75)

value, _ = tf.nn.dynamic_rnn(lstmCell, data, dtype=tf.float32) #last 
hidden state

我尝试按如下方式更改 lstm 模型:

lstmCell = tf.contrib.rnn.BasicLSTMCell(lstmUnits)
lstmCell = tf.contrib.rnn.DropoutWrapper(cell=lstmCell, 
      output_keep_prob=0.75)

def make_cell():
       return tf.contrib.rnn.BasicLSTMCell(lstmUnits)

cell = tf.contrib.rnn.MultiRNNCell(
    [make_cell() for _ in range(num_layers)], state_is_tuple=True)

initial_state = cell.zero_state(batchSize, tf.float32)
state = initial_state

for time_step in range(maxlen):
   if time_step > 0:
       tf.get_variable_scope().reuse_variables()   
   cell_out, state = cell(data[:, time_step, :], state)

错误如下:

TypeError: in converted code:
    relative to /opt/conda/lib/python3.6/site- 
    packages/tensorflow/python:

ops/rnn_cell_impl.py:767 call
    array_ops.concat([inputs, h], 1), self._kernel)
util/dispatch.py:180 wrapper
    return target(*args, **kwargs)
ops/array_ops.py:1299 concat
    return gen_array_ops.concat_v2(values=values, axis=axis, 
        name=name)
ops/gen_array_ops.py:1256 concat_v2
    "ConcatV2", values=values, axis=axis, name=name)
framework/op_def_library.py:499 _apply_op_helper
    raise TypeError("%s that don't all match." % prefix)

TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have 
     types [float64, float32] that don't all match.

最佳答案

最有可能的是,embedding_matrix_glovedtypefloat64,因为您的 data 正在变成 float64 最后你遇到了这个问题。将您的 embedding_matrix_glove 转换为 float32,然后您的问题就应该得到解决。

关于python - lstm tf.float64 tf.float32 之间存在转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58195931/

相关文章:

python - 什么是 ngram 计数以及如何使用 nltk 实现?

python - 在 matplotlib 中,如何使单条线的线宽随 x 变化?

python - Tensorflow 模型评估基于批量大小

python - TensorFlow dynamic_rnn 状态

perl - 如何更改属性类型? (珀尔哞)

python - 如何启动 http 服务器,然后打开 Web 浏览器?

python - 如何测量python中每个代码部分的RAM使用情况?

python - 在打印变量类型时,如何在 Tensorflow 中获得 pretty-print 结果?

c# - 在 .NET 中,在运行时 : How to get the default value of a type from a Type object?

c++ - "fun"和 "&fun"之间的类型差异?