简体   繁体   中英

Tensorflow getting ' ValueError: Exception encountered when calling layer "normalization" Dimensions must be equal'

I am following Tensorflow's regression tutorial and have created a multivariable linear regression and deep neural network however, when I am trying to collect the test set in test_results , I get the following error:

    ValueError: Exception encountered when calling layer "normalization" (type Normalization).

    Dimensions must be equal, but are 7 and 8 for '{{node sequential/normalization/sub}} = Sub[T=DT_FL    Dimensions must be equal, but are 7 and     Dimensions must be equal, but are 7 and 8 for '{{node sequential/normalization/sub}} = Sub[T=DT_FLOAT](sequential/Cast, sequential/normalizati
on/sub/y)' with input shapes: [?,7], [1,8].

    Call arguments received by layer "normalization" (type Normalization):
      • inputs=tf.Tensor(shape=(None, 7), dtype=float32)

Here is the some of code for the linear regression,starting from splitting labels, the error appears on the last line, test_results['linear_model'] = linear_model.evaluate(test_features, test_labels, verbose = 0) However, I am able to generate the error plots and everything seems to work fine otherwise, so I'm not entirely sure what the error is with getting test results. Any help would be much appreciated!

#Split labels
train_features = train_dataset.copy()
test_features = test_dataset.copy()

train_labels = train_features.pop('HCO3')
test_labels = test_features.pop('HCO3')

train_features = np.asarray(train_dataset.copy()).astype('float32')
#print(train_features.tail())

#Normalization
normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(np.array(train_features))
first = np.array(train_features[:1])

linear_model = tf.keras.Sequential([
    normalizer,
    layers.Dense(units=1)
])
#Compilation
linear_model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.1),
    loss='mean_absolute_error'
)
history = linear_model.fit(
    train_features,
    train_labels,
    epochs=100,
    # Suppress logging.
    verbose=0,
    # Calculate validation results on 20% of the training data.
    validation_split = 0.2)

#Track error for later
test_results = {}
test_results['linear_model'] = linear_model.evaluate(test_features, test_labels, verbose = 0)

You lost the outcome column in the dataframe because of pop. Try extracting that column using

train_labels = train_features['HC03']
test_labels = test_features['HC03']

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