簡體   English   中英

TensorFlow模型獲得零損失

[英]TensorFlow model gets zero loss

import tensorflow as tf
import numpy as np
import os
import re
import PIL


def read_image_label_list(img_directory, folder_name):
    # Input:
    #   -Name of folder (test\\\\train)
    # Output:
    #   -List of names of files in folder
    #   -Label associated with each file

    cat_label = 1
    dog_label = 0
    filenames = []
    labels = []

    dir_list = os.listdir(os.path.join(img_directory, folder_name))  # List of all image names in 'folder_name' folder

    # Loop through all images in directory
    for i, d in enumerate(dir_list):
        if re.search("train", folder_name):
            if re.search("cat", d):  # If image filename contains 'Cat', then true
                labels.append(cat_label)
            else:
                labels.append(dog_label)
        filenames.append(os.path.join(img_dir, folder_name, d))

    return filenames, labels


# Define convolutional layer
def conv_layer(input, channels_in, channels_out):
    w_1 = tf.get_variable("weight_conv", [5,5, channels_in, channels_out], initializer=tf.contrib.layers.xavier_initializer())
    b_1 = tf.get_variable("bias_conv", [channels_out], initializer=tf.zeros_initializer())
    conv = tf.nn.conv2d(input, w_1, strides=[1,1,1,1], padding="SAME")
    activation = tf.nn.relu(conv + b_1)
    return activation


# Define fully connected layer
def fc_layer(input, channels_in, channels_out):
    w_2 = tf.get_variable("weight_fc", [channels_in, channels_out], initializer=tf.contrib.layers.xavier_initializer())
    b_2 = tf.get_variable("bias_fc", [channels_out], initializer=tf.zeros_initializer())
    activation = tf.nn.relu(tf.matmul(input, w_2) + b_2)
    return activation


# Define parse function to make input data to decode image into
def _parse_function(img_path, label):
    img_file = tf.read_file(img_path)
    img_decoded = tf.image.decode_image(img_file, channels=3)
    img_decoded.set_shape([None,None,3])
    img_decoded = tf.image.resize_images(img_decoded, (28, 28), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    img_decoded = tf.image.per_image_standardization(img_decoded)
    img_decoded = tf.cast(img_decoded, dty=tf.float32)
    label = tf.one_hot(label, 1)
    return img_decoded, label


tf.reset_default_graph()

# Define parameterspe
EPOCHS = 10
BATCH_SIZE_training = 64
learning_rate = 0.001
img_dir = 'C:/Users/tharu/PycharmProjects/cat_vs_dog/data'
batch_size = 128

# Define data
features, labels = read_image_label_list(img_dir, "train")

# Define dataset
dataset = tf.data.Dataset.from_tensor_slices((features, labels))  # Takes slices in 0th dimension
dataset = dataset.map(_parse_function)
dataset = dataset.batch(batch_size)
iterator = dataset.make_initializable_iterator()

# Get next batch of data from iterator
x, y = iterator.get_next()

# Create the network (use different variable scopes for reuse of variables)
with tf.variable_scope("conv1"):
    conv_1 = conv_layer(x, 3, 32)
    pool_1 = tf.nn.max_pool(conv_1, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")

with tf.variable_scope("conv2"):
    conv_2 = conv_layer(pool_1, 32, 64)
    pool_2 = tf.nn.max_pool(conv_2, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")
    flattened = tf.contrib.layers.flatten(pool_2)

with tf.variable_scope("fc1"):
    fc_1 = fc_layer(flattened, 7*7*64, 1024)
with tf.variable_scope("fc2"):
    logits = fc_layer(fc_1, 1024, 1)


# Define loss function
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=tf.cast(y, dtype=tf.int32)))

# Define optimizer
train = tf.train.AdamOptimizer(learning_rate).minimize(loss)


with tf.Session() as sess:
    # Initiliaze all the variables
    sess.run(tf.global_variables_initializer())

    # Train the network
    for i in range(EPOCHS):
        # Initialize iterator so that it starts at beginning of training set for each epoch
        sess.run(iterator.initializer)
        print("EPOCH", i)
        while True:
            try:
                _, epoch_loss = sess.run([train, loss])

            except tf.errors.OutOfRangeError:  # Error given when out of data
                if i % 2 == 0:
                    # [train_accuaracy] = sess.run([accuracy])
                    # print("Step ", i, "training accuracy = %{}".format(train_accuaracy))
                    print(epoch_loss)
                break

我花了幾個小時試圖系統地弄清楚為什么我在運行此模型時會虧損0。

  • 功能=每個圖像的文件位置列表(例如['\\ data \\ train \\ cat.0.jpg',/ data \\ train \\ cat.1.jpg])
  • 標簽= [Batch_size,1] one_hot向量

最初我以為是因為我的數據有問題。 但是我在調​​整大小后查看了數據,並且圖像看起來還不錯。

然后我嘗試了一些不同的損失函數,因為我想可能是我誤會了張量流函數softmax_cross_entropy作用,但這並不能解決任何問題。

我試過只運行“登錄”部分以查看輸出是什么。 這只是一個小樣本,數字對我來說似乎很好:

 [[0.06388957]
 [0.        ]
 [0.16969752]
 [0.24913025]
 [0.09961276]]

假設相應的標簽為0或1,那么softmax_cross_entropy函數當然應該能夠計算此損失? 我不確定是否遺漏了一些東西。 任何幫助將不勝感激。

記錄所示

logitslabels必須具有相同的形狀,例如[batch_size, num_classes]和相同的float16float16float32float64 )。

由於您提到的標簽是“ [Batch_size,1] one_hot向量”,因此我假設您的logitslabels都是[Batch_size,1]形狀。 這肯定會導致零損失。 從概念上講,您只有1個班級( num_classes=1 ),並且您不會記錯( loss=0 )。

因此,至少對於labels ,應該對其進行轉換: tf.one_hot(indices=labels, depth=num_classes) 您的預測對logits也應具有形狀[batch_size, num_classes]輸出。

或者,您可以使用sparse_softmax_cross_entropy_with_logits ,其中:

一個常見的用例是具有形狀[batch_size,num_classes]的登錄名和形狀[batch_size]的標簽。 但是支持更高的尺寸。

暫無
暫無

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

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