Home>

An error occurs in machine learning.
Please let me know if there are any errors and where to fix

Source

import tensorflow as tf
import tensorflow.keras as keras ### Change
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
# Load data --- (* 1)
analysisresults_data = pd.read_csv ("analysis_resultstableZZZ.csv", encoding = "utf-8")
# Separate data into labels and input data
y = analysisresults_data.loc [:, "analysis_result"]
x = analysisresults_data.loc [:, ["id", "signatures_id", "hit_count"]]

# Divide for learning and testing --- (* 2)
x_train, x_test, y_train, y_test = train_test_split (x, y, test_size = 0.2, train_size = 0.8, shuffle = True)
# Define model structure --- (* 3)
Dense = keras.layers.Dense
model = keras.models.Sequential ()
model.add (Dense (10, activation = 'relu', input_shape = (3,)))
model.add (Dense (2, activation = 'softmax')) # --- (* 3a)
# Build model --- (* 4)
model.compile (
    loss = 'categorical_crossentropy',
    optimizer = 'adam',
    metrics = ['accuracy'])
# Run learning --- (* 5)
model.fit (x_train, y_train,
    batch_size = 20,
    epochs = 300)
# Evaluate the model --- (* 6)
score = model.evaluate (x_test, y_test, verbose = 1)
print ('Accuracy rate =', score [1], 'loss =', score [0])

2019-11-30 15: 12: 46.613774: I tensorflow/core/platform/cpu_feature_guard.cc: 142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-11-30 15: 12: 46.625817: I tensorflow/compiler/xla/service/service.cc: 168] XLA service 0x7fa07afcb160 executing computations on platform Host. Devices:
2019-11-30 15: 12: 46.625834: I tensorflow/compiler/xla/service/service.cc: 175] StreamExecutor device (0): Host, Default Version
WARNING: tensorflow: Falling back from v2 loop because of error: Failed to find data adapter that can handle input:,
Traceback (most recent call last):
File "deep2.py", line 33, in
epochs = 300)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 728, in fit
use_multiprocessing = use_multiprocessing)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 642, in fit
shuffle = shuffle)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 2538, in _standardize_user_data
y, self._feed_loss_fns, feed_output_shapes)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 717, in check_loss_and_target_compatibility
'while using as losscategorical_crossentropy.'
ValueError: You are passing a target array of shape (7999, 1) while using as losscategorical_crossentropy.categorical_crossentropyexpects targets to be binary matrices (1s and 0s) of shape ( samples, classes) .If your targets are integer classes, you can convert them to the expected format via:

from keras.utils import to_categorical
y_binary = to_categorical (y_int)

Alternatively, you can use the loss functionsparse_categorical_crossentropyinstead, which does expect integer targets.

  • Answer # 1

      

    What part is the error content?

    It is as follows.

      

    ValueError: You are passing a target array of shape (7999, 1) while using as loss categorical_crossentropy.categorical_crossentropy expects targets to be binary matrices (1s and 0s) of shape (samples, classes). , you can convert them to the expected format via:

      

    from keras.utils import to_categorical
      y_binary = to_categorical (y_int)

      

    Alternatively, you can use the loss function sparse_categorical_crossentropy instead, which does expect integer targets.

    In short, it means to convert with to_categorical () or use sparse_categorical_crossentropy.