【问题标题】:Tensorflow data API and keras ModelTensorFlow 数据 API 和 keras 模型
【发布时间】:2020-11-17 17:34:45
【问题描述】:

我正在尝试使用 Dataset API 设置一个简单的 NN,但遇到了错误。现在我有以下内容:

这是神经网络:

def get_model(input_shape, n_outputs):
    _input = tf.keras.Input(shape=input_shape)
    x = tf.keras.layers.Dense(128, activation='relu')(_input)
    x = tf.keras.layers.Dense(64, activation='relu')(x)


    value_output = tf.keras.layers.Dense(1, activation='sigmoid', name='value_output')(x)

    model = tf.keras.Model(inputs=_input, outputs=[value_output])
    losses = {
        "value_output": 'mean_squared_error'
    }
    model.compile(loss=losses, optimizer='adam')
    return model

这是我在 NN 上使用的数据

def preproces(item):
        return item, 0.3    

instance = np.array([26])
dataset = tf.data.Dataset.from_tensor_slices([instance])
dataset = dataset.map(preprocess)
m.fit(dataset)

我不断收到此错误:

ValueError:没有为任何变量提供梯度:['dense_27/kernel:0', 'dense_27/bias:0', 'dense_28/kernel:0', 'dense_28/bias:0', 'value_output_9/kernel: 0', 'value_output_9/bias:0'].

不知道缺少什么,这是一个很简单的神经网络。

感谢您在高级方面的帮助

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    我会提到您的数据不一致:如果您的输入形状是 [1] - 您的输出形状应该是 [64]。

    我相应地更改了preprocess()

    但我无法重现您的错误:

    import tensorflow as tf
    def get_model(input_shape, n_outputs):
        _input = tf.keras.Input(shape=input_shape)
        x = tf.keras.layers.Dense(128, activation='relu')(_input)
        x = tf.keras.layers.Dense(64, activation='relu')(x)
    
    
        value_output = tf.keras.layers.Dense(1, activation='sigmoid', name='value_output')(x)
    
        model = tf.keras.Model(inputs=_input, outputs=[value_output])
        losses = {
            "value_output": 'mean_squared_error'
        }
        model.compile(loss=losses, optimizer='adam')
        return model
    
    def preprocess(item):
            return item, [0.3]*64    
    
    import numpy as np
    instance = np.array([26])
    dataset = tf.data.Dataset.from_tensor_slices([instance])
    dataset = dataset.map(preprocess)
    m = get_model([1], 1)
    m.compile(loss='mse')
    m.fit(dataset)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2019-06-04
      • 2020-08-09
      • 2017-12-17
      相关资源
      最近更新 更多