An error occurs in machine learning.
Please let me know if there are any errors and where to fix
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 , 'loss =', score )
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 loss
ValueError: You are passing a target array of shape (7999, 1) while using as loss
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 function
sparse_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.
- python - abnormal termination in machine learning using jupyter notebook
- python - machine learning feature extraction of time series data
- python - inconsistency in sample size of cnn machine learning with keras
- machine learning - an error occurred in the feature calculation by the intermediate layer output of cnn
- machine learning - keras, i want to use the squared error function for classification problems
- machine learning - error when learning yolo format original data set in googlecolab/yolov3/darknet environment
- python machine learning
- machine learning in python, breast cancer diagnosis
- python - i get an error when connecting to a voice channel with discordpy
- machine learning - when competing for image classification in kaggle, do the top prize winners create their own models?
- python 3x - best estimator: i get an error with no syntax, so please tell me what to do
- python - reinforcement learning of tag does not go well with open ai gym
- python - an error has occurred in yolo v3
- [python] i don't know how to solve the error
- python - error when scraping with selenium and firefox
- python - idle cannot resolve the "rootgeometry" error
- machine learning - does k-fold cross validation apply test data after pre-splitting it?
- python - tuple error does not resolve
- python max () arg is an empty sequence and an error occurs and it cannot be processed well
- python 3x - error after changing to csv file