使用我的模型在不使用keras的情况下预测张量流2的输出

时间:2020-09-05 21:48:24

标签: python tensorflow tensorflow2.0

我是TensorFlow的新手,我有一个非常基本的问题。我在先前的TensorFlow版本中找到了几个与此问题相关的帖子,但我无法使用我正在使用的TensorFlow 2的答案。我在原始站点的文档中找到的示例使用Keras。

现在,关于我的问题,例如,我仅使用TensorFlow而不使用Keras构建了自己的模型。我已经完成了模型的训练,现在我想使用训练好的模型来预测我提供的某些输入的输出。

为了学习使用TensorFlow 2,我开始非常简单。我被困在这里,如果有人为我提供解决方案,那将有很大的帮助。我已经附上了我的代码片段。

# Defining input and output placeholders
x = tf.compat.v1.placeholder(tf.float32, shape=(128, 128, 1, 1)) # Placeholder for input
y = tf.compat.v1.placeholder(tf.float32, shape=(128, 128, 1, 1)) # Placeholder for ground truth 

inp = np.ones([128,128,1,1]).astype(np.float32) # Dummy input dataset I made
                                                # I will use it both for input and ground truth to train my model
# My neural network model
pred = my_model(x)

# Defining my optimizer, I am using gradient descent and l2 norm loss
l2_loss = tf.nn.l2_loss(tf.subtract(pred, y), name=None)
optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate=0.000001

# Training the model
training_iters = 300
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
  sess.run(init)
  summary_writer = tf.compat.v1.summary.FileWriter('./Output', sess.graph)
  for i in range(training_iters):
        dataset_dict = {x: inp, y: batch_y}
        opt = sess.run(optimizer.minimize(l2_loss), feed_dict = dataset_dict)
        loss = sess.run(l2_loss, feed_dict={x: batch_x, y: batch_y})
        print(loss)
  summary_writer.close()

# Use my trained model to predict output for some input I give
# ??
# ??

0 个答案:

没有答案