如何将预加载的张量流常数加载到模型中

时间:2018-10-24 18:25:18

标签: python tensorflow

我在GPU上完成所有的Tensorflow数据生成和训练。这意味着我的数据集被表示为张量流常量。例如:

import tensorflow as tf

# Generate x and y on the GPU
x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)

这意味着当我去训练模型时,它看起来类似于以下代码。我已对其进行了极大的简化,以便可以在其他计算机上重新创建问题(基于bottom of the tensorflow low-level API tutorial中显示“完整程序”的代码)

import tensorflow as tf

# Declare constants
x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)

# Create model to train
linear_model = tf.layers.Dense(units=1)
y_pred = linear_model(x) # <---- my model
loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)
optimizer = tf.train.AdamOptimizer(.2)
train = optimizer.minimize(loss)

# Boilerplate code
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# Train the model to give the correct y given the x
for i in range(100):
  _, loss_value = sess.run((train, loss))
  print(loss_value, end=", ")

# Test our output to make sure it looks good
print()
print(sess.run(y_pred))

# Generate new data to test or train in the model
new_x = tf.constant([[0], [1], [2]], dtype=tf.float32)
new_y_true = tf.constant([[10], [11], [12]], dtype=tf.float32)

# Test the model on new_x and see if it looks similar to new_y_true and/or train based on new_x and new_y_true
# ??

完成训练模型(声明为y_pred的模型后,我想重用该线性模型,并在new_xnew_y_true数据上进行测试,甚至进行训练。我该怎么办?

我尝试用占位符替换xy_true,但是您不能将tf.Tensor对象放入feed_dict参数中。

1 个答案:

答案 0 :(得分:0)

我不确定为什么要在图形中定义所有数据,并且如果要使用比此玩具示例中给出的数据更多的数据来执行此操作,则会遇到问题。您无法使用张量填充占位符(请参见Tensorflow: How to feed a placeholder variable with a tensor?)。

我唯一想到的是可以完成这项工作的是将tf.cond与一个布尔占位符一起使用,以指示您是否要训练或测试。请记住,tf.cond速度很慢,您应该阅读它的文档,因为它对于执行哪些操作有一些不直观的行为。然后看起来像这样:

import tensorflow as tf

# Declare constants
x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
y_true = tf.constant([[0], [-1], [-2], [-3]], dtype=tf.float32)

new_x = tf.constant([[0], [1], [2]], dtype=tf.float32)
new_y_true = tf.constant([[10], [11], [12]], dtype=tf.float32)

# Define indicator if you want to train or test
is_train = tf.placeholder(dtype=tf.bool, shape=[], name="is_training")



# Define which input data to use depending if you are training or not
input_data = tf.cond(pred=is_train,
                 true_fn=lambda: x,
                 false_fn=lambda: new_x
)

# Create model to train
linear_model = tf.layers.Dense(units=1)
y_pred = linear_model(input_data)

loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)
optimizer = tf.train.AdamOptimizer(.2)
train =  optimizer.minimize(loss)

# Boilerplate code
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# Train the model to give the correct y given the x
for i in range(100):
  _, loss_value = sess.run((train, loss), feed_dict={is_train: True})
  print(loss_value, end=", ")

# Test our output to make sure it looks good
print()
print(sess.run(y_pred, feed_dict={is_train: False}))