Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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 是否可以在子类模型中使用Tensorflow Keras函数API?_Python_Tensorflow_Keras - Fatal编程技术网

Python 是否可以在子类模型中使用Tensorflow Keras函数API?

Python 是否可以在子类模型中使用Tensorflow Keras函数API?,python,tensorflow,keras,Python,Tensorflow,Keras,我试图创建一个keras模型,在模型本身调用它之前,需要对输入进行特殊的预处理。我正在进行子类化,因为模型只是复杂网络的一部分,所以我可以连接输出并直接从代码的其他部分访问模型行为……等等 我设计它的方式是在构造函数中使用keras函数API来链接层,如果我不定义调用方法,这些层工作得很好(它的行为似乎与我调用实例时通常使用fAPI的行为完全一样) 我的问题是,当我想定义call方法时,我不确定调用什么函数从构造函数访问编译模型的默认行为: import tensorflow as tf imp

我试图创建一个keras模型,在模型本身调用它之前,需要对输入进行特殊的预处理。我正在进行子类化,因为模型只是复杂网络的一部分,所以我可以连接输出并直接从代码的其他部分访问模型行为……等等

我设计它的方式是在构造函数中使用keras函数API来链接层,如果我不定义
调用
方法,这些层工作得很好(它的行为似乎与我调用实例时通常使用fAPI的行为完全一样)

我的问题是,当我想定义
call
方法时,我不确定调用什么函数从构造函数访问编译模型的默认行为:

import tensorflow as tf
import numpy as np

class MyModel(tf.keras.Model):

  def __init__(self, inputshape):
    inputs = tf.keras.Input(shape=inputshape)
    x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
    outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)

    super(MyModel, self).__init__(inputs=inputs, outputs=outputs)


  def call(self, inputs, training=False):
    reduced_input = tf.expand_dims(inputs['b'], axis=0)

    # Want to call my compiled self MODEL with 'reduced_input' as the input argument but not sure how...
    return something(reduced_input)


myModelInstance = MyModel(inputshape=(3,))
myInput = {'a': [1, 2], 'b': np.array([3, 4, 5]), 'c': 6}

# Example preprocessing that I want to implement from within the model when called. Won't be this simple
reduced_input  = tf.expand_dims(myInput['b'], axis=0)

print(myModelInstance(reduced_input ))
在这段代码中,我简化了构造函数和输入预处理(这里它只从输入中提取'b'元素并添加了一个批处理维度),但我的实际实现更复杂

我更喜欢a)避免在调用实例之前预处理数据,b)将模型子类化为
Model
,而不是将模型存储为class属性


有没有一种方法可以像我尝试的那样将模型子类化与函数API结合起来?

您应该这样做:

class MyModel(tf.keras.Model):

  def __init__(self, inputshape):
    super(MyModel, self).__init__()
    inputs = tf.keras.Input(shape=inputshape)
    x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
    outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
    self.model = tf.keras.Model(inputs, outputs)

  def call(self, inputs, training=False):
    reduced_input = tf.expand_dims(inputs['b'], axis=0)

    # Want to call my compiled self MODEL with 'reduced_input' as the input argument but not sure how...
    return self.model(reduced_input)


myModelInstance = MyModel(inputshape=(3,))
myInput = {'a': [1, 2], 'b': np.array([3, 4, 5]), 'c': 6}

# Example preprocessing that I want to implement from within the model when called. Won't be this simple
#reduced_input  = tf.expand_dims(myInput['b'], axis=0)

print(myModelInstance(myInput))

谢谢,我想知道是否有办法直接使用实例而不是将新模型存储为类属性。如果我按照建议的方式做的话,子类化模型可能就没有意义了。。。