0
votes

I've tried running the following code, but got this error:

File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 235, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 593, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 706, in _process_inputs
use_multiprocessing=use_multiprocessing)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 702, in init
x = standardize_function(x)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 660, in standardize_function
standardize(dataset, extract_tensors_from_dataset=False)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2346, in _standardize_user_data
all_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y) File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2572, in _build_model_with_inputs
self._set_inputs(cast_inputs)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2647, in _set_inputs
inputs = self._set_input_attrs(inputs)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\training\tracking\base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 2681, in _set_input_attrs
raise ValueError('Passing a dictionary input to a Sequential Model '
ValueError: Passing a dictionary input to a Sequential Model which doesn't have FeatureLayer as the first layer is an error.

Code:

# -*- coding: utf-8 -*-
import os
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf
import tensorflow_datasets as tfds

try:
    model = keras.models.load_model("passrockmodel.h5")
except:
    print('\nDownloading Train Dataset...\n')
    train_dataset = tfds.load(name="rock_you", split="train[:75%]")
    assert isinstance(train_dataset, tf.data.Dataset)

    print('\nDownloading Test Dataset...\n')
    test_dataset = tfds.load("rock_you", split='train[-25%:]')
    assert isinstance(test_dataset, tf.data.Dataset)

    model = tf.keras.Sequential([
      tf.keras.layers.Dense(128, activation='relu'),
      tf.keras.layers.Dense(128, activation='relu'),
      tf.keras.layers.Dense(1, activation='sigmoid'),
    ])

    model.compile(
        loss='binary_crossentropy',
        optimizer='adam',
        metrics=['accuracy'])


    model.fit(train_dataset, epochs=20)

    model.save("passrockmodel.h5")


test_loss, test_accuracy = model.evaluate(test_dataset)

print('\nPredicting...\n')

predictions = model.predict(test_dataset)

print(predictions[0])
1
Does specifying the input_shape parameter of the first dense layer model help?Jake Tae
@JST99 I tried writing: tf.keras.layers.Dense(128, activation='relu', input_shape=(train_dataset.shape[1],)), but got another error instead: AttributeError: 'DatasetV1Adapter' object has no attribute 'shape'Tomer Katzir
I'm replying on my phone without a laptop to reproduce your code, so forgive me if I'm leading you in the wrong direction. However, it seems like train_dataset is not in numpy format. To call numpy's shape attribute on train_dataset, you might want to do something like train_dataset = tfds.as_numpy(tfds.load(name="rock_you", split="train[:75%]")).Jake Tae
@JST99 I did only the as_numpy like you said, and got this error: ValueError: Please provide model inputs as a list or tuple of 2 or 3 elements: (input, target) or (input, target, sample_weights) Received {'password': <tf.Tensor: shape=(8,), dtype=int64, numpy=array([100, 115, 98, 123, 122, 51, 57, 50], dtype=int64)>} Tomer Katzir

1 Answers

2
votes

I've had your problem yesterday. Here's what solved it for me:

your first layer should be of type tf.keras.layers.DenseFeatures

This first layer must be instantiated with an array of tf.feature_column objects. It happens that all my columns were numeric so my array was:

featureColumns = [tf.feature_column.numeric_column(columnNames[i], normalizer_fn= lambda x: (x - mean[i])/std[i]) for i in range(len(columnNames[:-1]))]

Note: the normalizer_fn arg is very useful as you can see, as well. It can eliminate the need of any additional normalization preprocessing layer should you need it.

And so my layer became:

layers.DenseFeatures(feature_columns=featureColumns, trainable=True)

I believe this should solve the error mentioned above in your question. Which is quoted as

ValueError: Passing a dictionary input to a Sequential Model which doesn't have FeatureLayer as the first layer is an error.