gpt4 book ai didi

python - TensorFlow 到 Keras 张量

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:23 24 4
gpt4 key购买 nike

我很想用这个 blog's info 来混合 TensorFlow 张量和 Keras 张量:

但是当输出需要是 Keras 张量而不是 TensorFlow 张量时,问题出现在最后一层。有没有简单的方法来转换?还是有一个 Keras 函数可以双线性调整大小?

finalOut = predict_flow2
finalOut = tf.image.resize_bilinear(finalOut, tf.stack([h, w]), align_corners=True)
model = Model(input=input, output=finalOut)
model.summary()

错误信息:

TypeError: Output tensors to a Model must be Keras tensors. Found: Tensor("ResizeBilinear:0", shape=(?, 320, 1152, 2), dtype=float32)

最佳答案

我的意思是,我无法知道什么是 predict_flow2。我假设它是一个 Keras 张量,但如果不是,您可以概括我的答案。

模型由层组成,层不完全是函数。要像这样使用 TF 函数(或任何函数),您需要将它们包裹在 Lambda 层中:

import numpy as np
import tensorflow as tf
from keras import Input, Model
from keras.layers import Lambda


x = Input((224, 224, 3))
h, w = 299, 299

y = Lambda(lambda inputs: tf.image.resize_bilinear(inputs,
tf.stack([h, w]),
align_corners=True))(x)
model = Model(inputs=x, output=y)
model.summary()

p = model.predict(np.random.randn(1, 224, 224, 3))
print('shape:', p.shape)

输出:

Using TensorFlow backend.
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 224, 224, 3) 0
_________________________________________________________________
lambda_1 (Lambda) (None, 299, 299, 3) 0
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

shape: (1, 299, 299, 3)

关于python - TensorFlow 到 Keras 张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48956407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com