class Model:
def __init__(
self,
learning_rate,
num_layers,
size,
size_layer,
output_size,
forget_bias = 0.1,
):
def lstm_cell(size_layer):
return tf.compat.v1.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)
rnn_cells = tf.compat.v1.nn.rnn_cell.MultiRNNCell(
[lstm_cell(size_layer) for _ in range(num_layers)],
state_is_tuple = False,
)
self.X = tf.compat.v1.placeholder(tf.float32, (None, None, size))
self.Y = tf.compat.v1.placeholder(tf.float32, (None, output_size))
drop = tf.compat.v1.nn.rnn_cell.DropoutWrapper(
rnn_cells, output_keep_prob = forget_bias
)
self.hidden_layer = tf.compat.v1.placeholder(
tf.float32, (None, num_layers * 2 * size_layer)
)
self.outputs, self.last_state = tf.compat.v1.nn.dynamic_rnn(
drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32
)
self.logits = tf.compat.v1.layers.dense(self.outputs[-1], output_size)
self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))
self.optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(
self.cost
)
我想将上面的代码转换为相关的TensorFlow 2.x,而又不急于执行,有人可以帮忙吗?
我一直在尝试更改一些内容,例如:将tf.compat.v1.nn.rnn_cell.LSTMCell
更改为tf.keras.layers.LSTMCell
,将tf.compat.v1.nn.rnn_cell.MultiRNNCell
更改为tf.keras.layers.StackedRNNCells
,也将tf.compat.v1.nn.dynamic_rnn
更改为tf.keras.layers.RNN
我该怎么办?
答案 0 :(得分:0)
TensorFlow 1.x脚本无法直接与TensorFlow 2.x一起使用,但需要进行转换。
tf_upgrade_v2 --infile tensorflow_v1.py --outfile tensorflow_v2.py
如果一个文件夹中有多个文件,则可以使用以下命令。
tf_upgrade_v2 v1-code-folder code-upgraded-folder