簡體   English   中英

TensorFlow 低級 model(沒有 Keras 也沒有 Sklearn) - 在每一步都獲得損失 = 0 和准確度 = 100%

[英]TensorFlow low level model (no Keras nor Sklearn) - getting loss = 0 and accuracy = 100% at every steps

我正在嘗試使用 Tensorflow v1 訓練分類 model 而不使用 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))

Output:

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%

為什么我每一步的損失都等於0,為什么我的准確率總是等於100%?

PS:我將dtype=tf.float32)添加到tf_test_dataset = tf.constant(test_dataset, dtype=tf.float32)行,因為否則它不會運行,給我這個錯誤:

---------------------------------------------------------------------------
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,
--> 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)
--> 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" %
-> 1535           (dtype.name, value.dtype.name, value))
   1536     return value

ValueError: Tensor conversion requested dtype float64 for Tensor with dtype float32: <tf.Tensor 'Variable/read:0' shape=(12288, 1) dtype=float32>

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_33/1251474610.py in <module>
     36     # Predictions for the training, validation, and test data.
     37     train_prediction = tf.nn.softmax(logits)
---> 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:
--> 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(
-> 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,
-> 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,
--> 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,但這沒什么區別我仍然得到相同的結果

我一個人發現了這個問題:

我剛剛添加了一個類別:

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

將其更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM