Python keras层中的包裹张量流函数

Python keras层中的包裹张量流函数,python,python-3.x,keras,Python,Python 3.x,Keras,我试图在keras lambda层中使用tensorflow唯一函数()。 代码如下: 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)

我试图在keras lambda层中使用tensorflow唯一函数()。 代码如下:

    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(优化器='Adam',loss='mean_squared_error')**
我得到一个错误:

ValueError:Tensor转换请求使用的Tensor的数据类型为int32 dtype float32:'张量(“lambda_9_样本_权重_1:0”,形状=(?,), dtype=float32“


有人知道这里的错误是什么,或者使用张量流函数的不同方法吗

keras模型要求输出
float32
,但从
tf.unique
返回的
索引是
int32
。铸造可以解决您的问题。
另一个问题是unique需要一个扁平阵列<代码>重塑
修复此问题

将tensorflow导入为tf
从keras导入输入
从keras.layers导入Lambda
从keras.engine导入模型
def唯一_idx(x):
x=tf.重塑(x,[-1])
u、 索引=tf.唯一(x)
返回tf.cast(索引,tf.float32)
x=输入(形状=(1,))
y=Lambda(唯一的_idx)(x)
模型=模型(输入=x,输出=y)
compile(优化器='adam',loss='mse')