简体   繁体   中英

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

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?

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()

请记住,诸如model.fit()model.evaluate()函数需要传递标签。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM