简体   繁体   English

创建一个神经网络来分类 mnist 数据集,而不使用 keras。 错误:通过张量损失时需要磁带

[英]Create a neural network to classify mnist dataset without using keras. Error: tape is required when a Tensor loss is passed

Here is the code.这是代码。 I have done the forward pass but I receive an error anytime I run it and I don't know what the problem is.我已经完成了前向传递,但每当我运行它时都会收到一个错误,我不知道问题是什么。 I first of all create a batch with the features and labels, do the forward pass and try to use keras SGD optimizer.我首先使用特征和标签创建一个批次,进行前向传递并尝试使用 keras SGD 优化器。

This is the error I get:这是我得到的错误: 错误

And this is my code:这是我的代码:

import tensorflow as tf
from tensorflow.keras.utils import to_categorical
import numpy as np
def batches(batch_size, features, labels):
    """
    Create batches of features and labels
    :param batch_size: The batch size
    :param features: List of features
    :param labels: List of labels
    :return: Batches of (Features, Labels)
    """
    assert len(features) == len(labels)
    outout_batches = []
    
    sample_size = len(features)
    features = tf.Variable(features, dtype='float32')
    for start_i in range(0, sample_size, batch_size):
        end_i = start_i + batch_size
        batch = (features[start_i:end_i], labels[start_i:end_i])
        outout_batches.append(batch)
        
    return outout_batches


def get_logits(features, weights, biases):
# network's forward pass, multiply inputs with weight
    return tf.add(tf.matmul(features, weights), biases)


def get_cost(logits, labels):
# returns the cost of the pass
    return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))

def vectorize(features):
# reshapes the features to a vector for input
    return features.reshape(features.shape[0], features.shape[1] * features.shape[2])

(train_x, train_y), (test_x, test_y) = tf.keras.datasets.mnist.load_data()
train_x, test_x = train_x.astype('float32'), test_x.astype('float32')
train_x, test_y = train_x.astype('float32'), test_y.astype('float32')
train_y, test_y = to_categorical(train_y, 10), to_categorical(test_y, 10)
train_x = vectorize(train_x)

n_inputs = 28 * 28
n_classes = 10

weights = tf.Variable(tf.random.normal([n_inputs, n_classes]), dtype='float32', name='weights')
biases = tf.Variable(tf.random.normal([n_classes]), dtype='float32', name='biases')

batch_list = batches(32, train_x, train_y)
for x, y in batch_list:
    logits = get_logits(x, weights, biases)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
    opt = tf.keras.optimizers.SGD(learning_rate=0.001)
    optimizer = opt.minimize(loss=cost)
   

This is because your loss is a Tensor.这是因为您的损失是张量。 In the optimizer.minimize() the loss argument can be a Tensor or callable.optimizer.minimize()中,损失参数可以是张量或可调用的。 If a callable, loss should take no arguments and return the value to minimize.如果是可调用的,则损失不应采用 arguments 并返回值以最小化。 If loss is a Tensor, the tape argument must be passed .如果loss 是张量,则必须传递 tape 参数

So the modified code could be like this:所以修改后的代码可能是这样的:

for x, y in batch_list:
  with tf.GradientTape() as tape:
    logits = get_logits(x, weights, biases)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
    opt = tf.keras.optimizers.SGD(learning_rate=0.001)
  optimizer = opt.minimize(loss=cost, var_list=[weights,biases], tape=tape)

Hi i made a little change towards your code, not sure if it fits your situation but i would normally do it like this嗨,我对您的代码做了一些更改,不确定它是否适合您的情况,但我通常会这样做

optimizer = tf.keras.optimizers.SGD(learning_rate=0.001)

for x, y in batch_list:
    with tf.GradientTape() as tape:
        logits = get_logits(x, weights, biases)
        cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
        
    grads = tape.gradient(loss, [weights, biases])
    optimizer.apply_gradients(zip(grads, [weights, biases]))
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过张量损失时需要磁带 - Tape is required when a tensor loss is passed 我正在尝试使用 Keras 实现神经网络。 我的错误如下: - I am trying to implement a neural network using Keras. My error is listed below: 凯拉斯。 Siamese 网络和三元组损失 - Keras. Siamese network and triplet loss 使用keras下载mnist数据集时出现错误 - errors when dowloading mnist dataset using keras RBF-神经网络无法分类MNIST数据集 - RBF-Neural net can't classify MNIST dataset 将 Python 的 Keras 用于神经网络时的类型错误 - Type Error When Using Python's Keras for Neural Network 使用Tensorflow / Keras的神经网络中的负二项损失 - Negative Binomial Loss in Neural Network using Tensorflow / Keras 尝试在Keras中创建自定义损失函数时出现“仅当启用急切执行时,张量对象才可迭代”错误 - “Tensor objects are only iterable when eager execution is enabled” error when trying to create custom loss function in Keras 在sklearn / keras中使用cross_val_score时的负损失函数。 不使用k折时有效 - Negative Loss function when using cross_val_score in sklearn/keras. Works when not using k fold 将张量元组传递给多个损失函数时,Tensorflow/keras 仅使用.fit 传递元组中的一个张量 - Tensorflow/keras when passing tuple of tensors to multiple loss functions, only one tensor in tuple is being passed using .fit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM