Tensorflow 2.0 Beta: Model.fit() throws ValueError: Arguments and signature arguments do not match: 56 57

老子叫甜甜 提交于 2019-12-11 14:55:10

问题


I'm new to machine learning. I'm trying to make a simple RNN in Tensorflow 2.0 but I'm hitting a snag. I've reduced it to a minimal example that reproduces the problem. The goal of this minimal example is for the RNN to learn to output 1.0 repeatedly.

import os
import sys
import math
from random import shuffle
import numpy as np
import tensorflow as tf
from time import time as time

epochs = 200
batch_size = 32
chunk_length = 64
features = 10

def main():
    train_dataset = np.zeros([batch_size, chunk_length, features]) + 1
    test_dataset = np.zeros([batch_size, chunk_length, features]) + 1

    with tf.device('/gpu:0'):
        model = tf.keras.Sequential([
            tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(
                64, return_sequences=True)),
            tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
            tf.keras.layers.Dense(64, activation='relu'),
            tf.keras.layers.Dense(1, activation='sigmoid')
        ])
        model.compile(loss='mean_absolute_error',
            optimizer='adam',
            metrics=['accuracy'])

        history = model.fit(train_dataset, batch_size=batch_size, epochs=epochs)

        test_loss, test_acc = model.evaluate(test_dataset)

        print('Test Loss: {}'.format(test_loss))
        print('Test Accuracy: {}'.format(test_acc))

if __name__ == '__main__':
    main()

When I run this I get ValueError: Arguments and signature arguments do not match: 56 57. If I comment out the last layer, I get ValueError: Arguments and signature arguments do not match: 50 51. If I comment out the last two layers I get ValueError: Arguments and signature arguments do not match: 44 45.

I have tried modifying all of the constants I provide (epochs, batch_size, chunk_length, and features) but these have no effect on the error. I also tried removing the element-wise addition of 1 to the numpy arrays, but this also has no effect.

Is this a bug in TensorFlow or am I doing something stupid?


回答1:


I'm on tensorflow version 1.13.1 and no GPU, but hopefully this still fixes the problems. It seems like you are only feeding your network input data (x), but no respond data (y). So there is nothing for the model to learn. I only added the response data train_Y_dataset and test_Y_dataset. The following code worked for me in tensorflow 1.13.1, see comment for changes:

import os
import sys
import math
from random import shuffle
import numpy as np
import tensorflow as tf
from time import time as time

epochs = 200
batch_size = 32
chunk_length = 64
features = 10

def main():
    train_X_dataset = np.zeros([batch_size, chunk_length, features]) + 1
    # add train_Y_dataset:
    train_Y_dataset = np.zeros([batch_size, 1]) + 1
    test_X_dataset = np.zeros([batch_size, chunk_length, features]) + 1
    # add test_Y_dataset:
    test_Y_dataset = np.zeros([batch_size, 1]) + 1 #1

    #with tf.device('/gpu:0'):
    model = tf.keras.Sequential([
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(
            64, return_sequences=True)),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])
    model.compile(loss='mean_absolute_error',
        optimizer='adam',
        metrics=['accuracy'])

    # add the response variable train_Y_dataset in fit    
    history = model.fit(x=train_X_dataset, y=train_Y_dataset, batch_size=batch_size, epochs=epochs)

    # add the response variable test_Y_dataset in evaluate   
    test_loss, test_acc = model.evaluate(x=test_X_dataset, y=test_Y_dataset)

    print('Test Loss: {}'.format(test_loss))
    print('Test Accuracy: {}'.format(test_acc))

if __name__ == '__main__':
    main()



回答2:


Remember that functions such as model.fit() and model.evaluate() require labels to be passed in.



来源:https://stackoverflow.com/questions/56555084/tensorflow-2-0-beta-model-fit-throws-valueerror-arguments-and-signature-argu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!