0

I am trying to use CTC_Loss function in fashion_mnist dataset but I am not able to understand the parameters like y_true, y_pred, input_length and label_lengths in fashion_mnist dataset.

So far I tried the below code, but getting errors

from __future__ import absolute_import, division, print_function, unicode_literals

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
from keras import backend as K

print(tf.__version__)


fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images / 255.0

test_images = test_images / 255.0
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])
model.summary()

def ctc_loss(y_true, y_pred):

    return K.ctc_batch_cost(y_true, y_pred, input_length, label_length)

model.compile(optimizer='adam',
              loss=ctc_loss,
              metrics=['accuracy'])


def validate(model, test_images, test_labels):
    test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
    return (test_acc)
def train(model):
    epoch = 0
    earlystop = 5
    noImprovementSince = 0
    bestCharErrorRate = float('inf')
    while True:
        epoch += 1
        print('Epoch:', epoch)

        # train
        print('Train NN')
        model.fit(train_images, train_labels, epochs=epoch)

        # validate
        charErrorRate = validate(model, test_images, test_labels)

        # if best validation accuracy so far, save model parameters
        if charErrorRate < bestCharErrorRate:
            print('Character error rate improved, save model')
            bestCharErrorRate = charErrorRate
            noImprovementSince = 0
#             model.save()
#             open(FilePaths.fnAccuracy, 'w').write(
#                 'Validation character error rate of saved model: %f%%' % (charErrorRate * 100.0))
        else:
            print('Character error rate not improved')
            noImprovementSince += 1

        # stop training if no more improvement in the last x epochs
        if noImprovementSince >= earlystop:
            print('No more improvement since %d epochs. Training stopped.' % earlystop)
            break

    return model.save('/models')

train(model)

Here I can't understand the y_true, input_length and label_length.

I was trying this link

Nitin Zadage
  • 633
  • 1
  • 9
  • 27
Neo_21995
  • 67
  • 7

0 Answers0