Модель низкого уровня TensorFlow (без Keras и Sklearn) — получение потерь = 0 и точности = 100% на каждом шаге

#python #numpy #tensorflow #machine-learning #deep-learning

#питон #тупица #тензорный поток #машинное обучение #глубокое обучение

Вопрос:

Я пытаюсь обучить классификационную модель с использованием Tensorflow v1 без использования keras, sklearn или какой-либо другой библиотеки.

 # Imports import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O   import numbers import array from collections.abc import Iterable  import os,shutil, cv2, itertools, glob, random  import tensorflow.compat.v1 as tf tf.disable_v2_behavior()  import matplotlib.pyplot as plt  #from tensorflow import keras #from tensorflow.keras import layers  # DATASET CREATION  for dirname, _, filenames in os.walk('/kaggle/input'):  for filename in filenames:  print(os.path.join(dirname, filename))  
 #_______________________TRAINING-SET____________________________# path = '/kaggle/input/cat-and-dog/training_set/training_set/' paths = glob.glob(path "*/*.jpg") random.shuffle(paths)  x_train = [] y_train = []   for path in paths:   img = cv2.resize(cv2.imread(path), (64,64))  x_train.append(img)  y_train.append(path.split("/")[-2])  print("number of pictures picked in our TRAINSET : ",len(x_train))  #_______________________TEST-SET____________________________#  path_test = '/kaggle/input/cat-and-dog/test_set/test_set/' paths_test = glob.glob(path_test "*/*.jpg") random.shuffle(paths_test)  x_test = [] y_test = []   for path_test in paths_test:   #img = tf.image.rgb_to_grayscale(img)  img = cv2.resize(cv2.imread(path_test), (64,64))  # img = img.reshape((64,64))  x_test.append(img)  y_test.append(path_test.split("/")[-2])  print("number of pictures picked in our TESTSET: ",len(x_test))  

Выход:

 number of pictures picked in our TRAINSET : 8005 number of pictures picked in our TESTSET: 2023  
 def prepare(x,y):  dataset = np.array(x)/255.0 # Normalization of Data    y_array = np.array(y)  labels = np.zeros((len(x),1))   #binarize Y  i=0  for label in y_array:  if label == "dogs":  labels[i,0] = 0  else:  labels[i,0] = 1  i =1    print("dataset before reshape is {}".format(dataset.shape))  dataset=dataset.reshape(dataset.shape[0],-1)      return dataset,labels  
 --------------TRAIN--------------- dataset before reshape is (8005, 64, 64, 3) train_dataset reshaped is (8005, 12288) train_labels shape is (8005, 1) train_labels [[0.]  [1.]  [1.]  ...  [0.]  [1.]  [0.]] --------------TEST--------------- dataset before reshape is (2023, 64, 64, 3) test_dataset reshaped is (2023, 12288) test_labels shape is (2023, 1) test_labels [[1.]  [0.]  [1.]  ...  [1.]  [1.]  [0.]] ---------------------------------  

Output:

 --------------TRAIN--------------- train_dataset shape is (8005, 12288) train_labels shape is (8005, 1) train_labels [[1.]  [0.]  [1.]  ...  [1.]  [0.]  [1.]] --------------TEST--------------- test_dataset shape is (2023, 12288) test_labels shape is (2023, 1) test_labels [[1.]  [1.]  [1.]  ...  [0.]  [1.]  [0.]] ---------------------------------  
 # number of features num_features = len(train_dataset[1]) #12888 # number of target labels num_labels = len(train_labels[1]) #1 # learning rate (alpha) learning_rate = 0.05 # batch size batch_size = 20 # number of epochs num_steps = 3000   # initialize a tensorflow graph graph = tf.Graph()   with graph.as_default():    # defining all the nodes  # Inputs  tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, num_features))  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))  tf_test_dataset = tf.constant(test_dataset, dtype=tf.float32)   # Variables.  weights = tf.Variable(tf.truncated_normal([num_features, num_labels]))  biases = tf.Variable(tf.zeros([num_labels]))   # Training computation.  logits = tf.matmul(tf_train_dataset, weights)   biases  loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))   # Optimizer.  optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)   # Predictions for the training, validation, and test data.  train_prediction = tf.nn.softmax(logits)  test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights)   biases)  
 # utility function to calculate accuracy def accuracy(predictions, labels):  correctly_predicted = np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))  accu = (100.0 * correctly_predicted) / predictions.shape[0]  return accu   with tf.Session(graph=graph) as session:  # initialize weights and biases  tf.global_variables_initializer().run()  print("Initialized")    for step in range(num_steps):  # pick a randomized offset  offset = np.random.randint(0, train_labels.shape[0] - batch_size - 1)    # Generate a minibatch.  batch_data = train_dataset[offset:(offset   batch_size), :]  batch_labels = train_labels[offset:(offset   batch_size), :]    # Prepare the feed dict  feed_dict = {tf_train_dataset : batch_data,  tf_train_labels : batch_labels}    # run one step of computation  _, l, predictions = session.run([optimizer, loss, train_prediction],  feed_dict=feed_dict)    if (step % 500 == 0):  print("Minibatch loss at step {0}: {1}".format(step, l))  print("Minibatch accuracy: {:.1f}%".format(  accuracy(predictions, batch_labels)))    print("nTest accuracy: {:.1f}%".format(  accuracy(test_prediction.eval(), test_labels)))  

Output:

 Initialized Minibatch loss at step 0: 0.0 Minibatch accuracy: 100.0% Minibatch loss at step 500: 0.0 Minibatch accuracy: 100.0% Minibatch loss at step 1000: 0.0 Minibatch accuracy: 100.0% Minibatch loss at step 1500: 0.0 Minibatch accuracy: 100.0% Minibatch loss at step 2000: 0.0 Minibatch accuracy: 100.0% Minibatch loss at step 2500: 0.0 Minibatch accuracy: 100.0%  Test accuracy: 100.0%  

Why do I get a loss equals to 0 at every step, and why is my accuracy always equal to 100% ?

PS: I added dtype=tf.float32) to the line tf_test_dataset = tf.constant(test_dataset, dtype=tf.float32) because it would not run otherwise, throwing me this error:

 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)  521 as_ref=input_arg.is_ref, --gt; 522 preferred_dtype=default_dtype)  523 except TypeError as err:  /opt/conda/lib/python3.7/site-packages/tensorflow/python/profiler/trace.py in wrapped(*args, **kwargs)  162 return func(*args, **kwargs) --gt; 163 return func(*args, **kwargs)  164   /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)  1534 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" % -gt; 1535 (dtype.name, value.dtype.name, value))  1536 return value  ValueError: Tensor conversion requested dtype float64 for Tensor with dtype float32: lt;tf.Tensor 'Variable/read:0' shape=(12288, 1) dtype=float32gt;  During handling of the above exception, another exception occurred:  TypeError Traceback (most recent call last) /tmp/ipykernel_33/1251474610.py in lt;modulegt;  36 # Predictions for the training, validation, and test data.  37 train_prediction = tf.nn.softmax(logits) ---gt; 38 test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights)   biases)  39   /opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)  204 """Call target, and fall back on dispatchers if there is a TypeError."""  205 try: --gt; 206 return target(*args, **kwargs)  207 except (TypeError, ValueError):  208 # Note: convert_to_eager_tensor currently raises a ValueError, not a  /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py in matmul(a, b, transpose_a, transpose_b, adjoint_a, adjoint_b, a_is_sparse, b_is_sparse, output_type, name)  3653 else:  3654 return gen_math_ops.mat_mul( -gt; 3655 a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)  3656   3657   /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py in mat_mul(a, b, transpose_a, transpose_b, name)  5712 _, _, _op, _outputs = _op_def_library._apply_op_helper(  5713 "MatMul", a=a, b=b, transpose_a=transpose_a, transpose_b=transpose_b, -gt; 5714 name=name)  5715 _result = _outputs[:]  5716 if _execute.must_record_gradient():  /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)  556 "%s type %s of argument '%s'." %  557 (prefix, dtypes.as_dtype(attrs[input_arg.type_attr]).name, --gt; 558 inferred_from[input_arg.type_attr]))  559   560 types = [values.dtype]  TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type float64 of argument 'a'.  

Может ли это быть связано с проблемой потери/точности ?

РЕДАКТИРОВАТЬ Кто-то предложил увеличить размер пакета с 20 до 4000, но это не имеет значения, я все равно получаю те же результаты

Ответ №1:

Я нашел проблему в одиночку:

Я только что добавил категорию:

 labels = np.zeros((len(x),1))  

изменил его на:

 labels = np.zeros((len(x),2))