我正在尝试在keras lambda层中使用tensorflow唯一函数(https://www.tensorflow.org/api_docs/python/tf/unique) . 代码如下:
def unique_idx(x):
output = tf.unique(x)
return output[1]
then
inp1 = Input(batch_shape(None, 1))
idx = Lambda(unique_idx)(inp1)
model = Model(inputs=inp1, outputs=idx)
当我现在使用 **model.compile(optimizer='Adam', loss='mean_squared_error')**
我收到错误:
ValueError:Tensor转换请求dtype int32 for Tensor with dtype float32:'Tensor(“lambda_9_sample_weights_1:0”,shape =(?,),dtype = float32)'
有谁知道这里的错误或使用张量流函数的不同方式?
1 回答
keras模型需要
float32
作为输出,但tf.unique
返回的indices
是int32
. 一个演员可以解决您的问题 .另一个问题是,unique需要一个扁平阵列 .
reshape
解决了这个问题 .