Closed PotnisAkshay closed 3 years ago
Hi, sorry I'm not sure, I do not have much experience with text data and text models. My guess is that there might be some error in:
Option A: part where you create model inputs. Option B: part where you preprocess data.
Maybe try posting this question on StackOverflow with tensorflow
tag? There is a higher chance that more people will see it and that someone will help.
Hi, Thank you for the suggestions. I have posted this on stackOverflow with the tensorflow tag. I will let you know if I receive any responses. Till then I am closing this issue.
**Hi,
I am facing an issue with my code which uses a Keras functional API and a Keras Tuner. I didn't know where else to post this. Below is my code**
import numpy as np import pandas as pd import tensorflow as tf
from sklearn.model_selection import train_test_split from tensorflow.keras import layers from tensorflow.keras.layers.experimental import preprocessing
import os import matplotlib.pyplot as plt import pandas as pd import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow.keras.layers.experimental import preprocessing import seaborn as sns import numpy as np
pip install -q -U keras-tuner
import keras_tuner as kt
df = pd.read_csv("/content/credit-approval_csv.csv", delimiter=',')
train, test = train_test_split(df, test_size=0.2) train, val = train_test_split(train, test_size=0.2) print(len(train), 'train examples') print(len(val), 'validation examples') print(len(test), 'test examples')
def df_to_dataset(dataframe, shuffle=True, batch_size=32): dataframe = df.copy() labels = dataframe.pop('class') ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels)) if shuffle: ds = ds.shuffle(buffer_size=len(dataframe)) ds = ds.batch(batch_size) ds = ds.prefetch(batch_size) return ds
def get_normalization_layer(name, dataset):
Create a Normalization layer for our feature.
normalizer = preprocessing.Normalization(axis=None)
Prepare a Dataset that only yields our feature.
feature_ds = dataset.map(lambda x, y: x[name])
Learn the statistics of the data.
normalizer.adapt(feature_ds)
return normalizer
def get_category_encoding_layer(name, dataset, dtype, max_tokens=None):
Create a StringLookup layer which will turn strings into integer indices
if dtype == 'string': index = preprocessing.StringLookup(max_tokens=max_tokens) else: index = preprocessing.IntegerLookup(max_tokens=max_tokens)
Prepare a Dataset that only yields our feature
feature_ds = dataset.map(lambda x, y: x[name])
Learn the set of possible values and assign them a fixed integer index.
index.adapt(feature_ds)
Create a Discretization for our integer indices.
encoder = preprocessing.CategoryEncoding(num_tokens=index.vocabulary_size())
Apply one-hot encoding to our indices. The lambda function captures the
layer so we can use them, or include them in the functional model later.
return lambda feature: encoder(index(feature))
batch_size = 256 train_ds = df_to_dataset(train, batch_size=batch_size) val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size) test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)
all_inputs = [] encoded_features = []
Numeric features.
for header in ['A2', 'A3', 'A8', 'A11', 'A14', 'A15']: numeric_col = tf.keras.Input(shape=(15), name=header) normalization_layer = get_normalization_layer(header, train_ds) encoded_numeric_col = normalization_layer(numeric_col) all_inputs.append(numeric_col) encoded_features.append(encoded_numeric_col)
Categorical features encoded as string.
categorical_cols = ['A13', 'A12', 'A10', 'A9', 'A7', 'A6', 'A5', 'A4', 'A1'] for header in categorical_cols: categorical_col = tf.keras.Input(shape=(15), name=header, dtype='string') encoding_layer = get_category_encoding_layer(header, train_ds, dtype='string', max_tokens=5) encoded_categorical_col = encoding_layer(categorical_col) all_inputs.append(categorical_col) encoded_features.append(encoded_categorical_col)
def build_model(hp): hp_units = hp.Int('units', min_value=1, max_value=1512, step=32) all_features = tf.keras.layers.concatenate(encoded_features) dense = layers.Dense(units=hp_units, activation="relu") x = dense(all_features) x = layers.Dense(units=hp_units, activation="relu")(x) x = layers.Dense(units=hp_units, activation="relu")(x) x = layers.Dense(units=hp_units, activation="relu")(x) x = layers.Dropout(rate=0.5)(x) outputs = layers.Dense(units=hp_units)(x)
model = tf.keras.Model(all_inputs, outputs)
hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
optimizer = hp.Choice("optimizer", ["adam", "sgd", "RMSprop"]) loss = hp.Choice("loss", ["BinaryCrossentropy", "CategoricalCrossentropy", "SparseCategoricalCrossentropy"])
model.compile(optimizer, loss, metrics=['accuracy'])
return model
tuner = kt.Hyperband(build_model, objective='val_accuracy', max_epochs=10, factor=3, hyperband_iterations=1, directory='my_dir', project_name='intro_to_kt', overwrite=True)
tuner.search(train_ds, epochs=50, validation_data=val_ds)`
After this I run into the below error
`Epoch 1/2 WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.float32, name='A2'), name='A2', description="created by layer 'A2'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.float32, name='A3'), name='A3', description="created by layer 'A3'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.float32, name='A8'), name='A8', description="created by layer 'A8'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.float32, name='A11'), name='A11', description="created by layer 'A11'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.float32, name='A14'), name='A14', description="created by layer 'A14'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.float32, name='A15'), name='A15', description="created by layer 'A15'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A13'), name='A13', description="created by layer 'A13'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A12'), name='A12', description="created by layer 'A12'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A10'), name='A10', description="created by layer 'A10'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A9'), name='A9', description="created by layer 'A9'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A7'), name='A7', description="created by layer 'A7'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A6'), name='A6', description="created by layer 'A6'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A5'), name='A5', description="created by layer 'A5'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A4'), name='A4', description="created by layer 'A4'"), but it was called on an input with incompatible shape (None, 1). WARNING:tensorflow:Model was constructed with shape (None, 15) for input KerasTensor(type_spec=TensorSpec(shape=(None, 15), dtype=tf.string, name='A1'), name='A1', description="created by layer 'A1'"), but it was called on an input with incompatible shape (None, 1).
ValueError Traceback (most recent call last)