Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

python - Model.fit() is returning "This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data"

When running a model.fit(train_ds, epochs=10, validation_data=val_ds), I'm receiving the error "ValueError: This model has not yet been built. Build the model first by calling build() or calling fit() with some data, or specify an input_shape argument in the first layer(s) for automatic build."

I'm very confused, because i'm trying to run exactly what the error is suggesting that needs to happen. I've followed the structured data tutorial to the T.

I've verified that the datasets I'm passing contain data

Any help would be greatly appreciated... I'm spinning my wheels. Tried both tf 2.3.* and 2.4.*

customersDf = pd.DataFrame({"Name":['joe','frank','bob','eleanore'],'target':[1,1,2,2,]})
train, test = train_test_split(customersDf, test_size = 0.2)
train, val = train_test_split(train, test_size = 0.2)
print(len(train))
print(len(test))
print(len(val))

batch_size = 2048

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)

feature_columns = []

name1 = feature_column.categorical_column_with_vocabulary_list(
      'Name', customersDf.Name.unique())

name1_embedding = feature_column.embedding_column(name1, dimension=12)

feature_columns.append(name1_embedding)

feature_layer = tf.keras.layers.DenseFeatures(feature_columns)

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

model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])
              
model.fit(train_ds, epochs=10, validation_data=val_ds)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...