Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对keras层使用tf.split或tf.slice_Python_Python 3.x_Tensorflow_Keras - Fatal编程技术网

Python 对keras层使用tf.split或tf.slice

Python 对keras层使用tf.split或tf.slice,python,python-3.x,tensorflow,keras,Python,Python 3.x,Tensorflow,Keras,我想将大小为[batchSize,2,16]的训练样本拆分为大小为[batchSize,2]的16个张量,并将它们输入到同一个模型中。我如何在keras中实现这些目标 我首先以以下方式实现了这一点: def functionA(x_Config): y = layers.Input(shape=(2,16,)) hidden1 = 5 hidden2 = 10 x_input = layers.Input(shape=(2,)) hidden_lay

我想将大小为
[batchSize,2,16]
的训练样本拆分为大小为
[batchSize,2]
的16个张量,并将它们输入到同一个模型中。我如何在keras中实现这些目标

我首先以以下方式实现了这一点:

def functionA(x_Config):

    y = layers.Input(shape=(2,16,))

    hidden1 = 5
    hidden2 = 10

    x_input = layers.Input(shape=(2,))
    hidden_layer1 = Dense(hidden1, activation='relu')(x_input)
    hidden_layer2 = Dense(hidden2, activation='relu')(hidden_layer1)
    x_output = Dense(x_Config.m, activation='linear')(hidden_layer2)
    model_x= Model(inputs=x_input, outputs=x_output)


    for i in range(16):
        x_input = Lambda(lambda x: x[:, :, i])(y)

        if i == 0:
           x_output = model_x(x_input)
        else:
            x_output = layers.concatenate([x_output, 
                                      model_x(x_input)])

    x_output = Lambda(lambda x: x[:, :tf.cast(N, tf.int32)])(x_output)

    final_model = Model(y, x_output)

    return final_model
PS:对于
[batchSize,2]
输入(无需拆分),我有一个针对
model_x
的相同NN架构的经过培训的模型。这种型号性能很好。当我尝试将该模型的权重加载到上述代码中时,
model_x
,它根本不能提供良好的性能,也不能很好地进行训练

所以我相信我的问题在Lamda层的循环中。如何使用
tf.split
tf.slice
进行此操作?

您可以使用函数:

tensors = tf.unstack(data, axis=2)
您可以使用以下功能:

tensors = tf.unstack(data, axis=2)