Reputation: 1820
I have written out a simple single TFRECORDS file that contains three features and a label. As I am following tutorials, it seems that to use these TFRECORDS I need to create a dataset, parse the examples, do other things like normalization through map(). If this is not correct workflow I would be grateful to hear!
dataset = tf.data.TFRecordDataset("dataset.tfrecords")
#parse the protobuffer
def _parse_function(proto):
# define your tfrecord again.
keys_to_features = {'weight_pounds': tf.io.FixedLenFeature([], tf.float32),
'gestation_weeks': tf.io.FixedLenFeature([], tf.float32),
'plurality': tf.io.FixedLenFeature([], tf.float32),
'isMale': tf.io.FixedLenFeature([], tf.float32),
}
# Load one example
parsed_features = tf.io.parse_example(proto, keys_to_features)
# Turn your saved image string into an array
#parsed_features['image'] = tf.decode_raw(
# parsed_features['image'], tf.uint8)
return parsed_features
hold_meanstd={
'weight_pounds':[7.234738,1.330294],
'gestation_weeks':[38.346464,4.153269],
'plurality':[1.035285,0.196870]
}
def normalize(example):
example['weight_pounds']=(example['weight_pounds']-hold_meanstd['weight_pounds'][0])/hold_meanstd['weight_pounds'][1]
example['gestation_weeks']=(example['gestation_weeks']-hold_meanstd['gestation_weeks'][0])/hold_meanstd['gestation_weeks'][1]
example['plurality']=(example['plurality']-hold_meanstd['plurality'][0])/hold_meanstd['plurality'][1]
label=example.pop('isMale')
return(example,label)
dataset = tf.data.TFRecordDataset(["dataset.tfrecords"]).map(_parse_function)
dataset =dataset.map(normalize)
dataset =dataset.batch(64)
Then once I have this dataset, I was thinking I could feed into a Keras model:
Dense = keras.layers.Dense
model = keras.Sequential(
[
Dense(500, activation="relu", kernel_initializer='uniform',
input_shape=(3,)),
Dense(200, activation="relu"),
Dense(100, activation="relu"),
Dense(25, activation="relu"),
Dense(1, activation="sigmoid")
])
optimizer = keras.optimizers.RMSprop(lr=0.01)
# Compile Keras model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=[tf.keras.metrics.AUC()])
model.fit(dataset)
This throws an error:
ValueError: Layer sequential_1 expects 1 inputs, but it received 3 input tensors. Inputs received: [<tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=float32>, <tf.Tensor 'ExpandDims_1:0' shape=(None, 1) dtype=float32>, <tf.Tensor 'ExpandDims_2:0' shape=(None, 1) dtype=float32>]
The issue seems to be that the input dataset looks like three inputs instead of one? How to allow Keras to train on a TF RECORDS dataset?
Upvotes: 0
Views: 1052
Reputation: 11631
After specifying input_shape=(3,)
in your first Dense layer, your keras model expects as an input a Tensor with the shape (None,3)
(where None
defines the batch size). We can take for example the following:
[
[0.0,0.0,0.0]
]
If we look a your tf.data.Dataset
, we can see that it is returning a dictionary. Each input will look like:
{
"weight_pounds":[0.0],
"gestation_weeks":[0.0],
"plurality":[0.0]
}
which is a bit different from the input_shape
specified above!
To fix that, you have two solution:
def normalize(example):
example['weight_pounds']=(example['weight_pounds']-hold_meanstd['weight_pounds'][0])/hold_meanstd['weight_pounds'][1]
example['gestation_weeks']=(example['gestation_weeks']-hold_meanstd['gestation_weeks'][0])/hold_meanstd['gestation_weeks'][1]
example['plurality']=(example['plurality']-hold_meanstd['plurality'][0])/hold_meanstd['plurality'][1]
label=example.pop('isMale')
# removing the dict struct
data_input = [example['weight_pounds'], example['gestation_weeks'], example['plurality']]
return(data_input,label)
from keras.layers import Dense, DenseFeatures
from tensorflow.feature_column import numeric_column
feature_names = ['weight_pounds', 'gestation_weeks', 'plurality']
columns = [numeric_column(header) for header in feature_names]
model = keras.Sequential(
[
DenseFeatures(columns)
Dense(500, activation="relu", kernel_initializer='uniform'),
Dense(200, activation="relu"),
Dense(100, activation="relu"),
Dense(25, activation="relu"),
Dense(1, activation="sigmoid")
])
Upvotes: 3