TypeError: Value passed to parameter ‘a‘ has DataType uint8 not in list of allowed values:

今天在做自定义全连接层遇到了这个问题,数据类型与权重参数矩阵不匹配。

因为手写数字识别是二值图像,加上这个x_train = x_train/255.0,就好了

import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras import Model, layers
import tensorflow as tf
import numpy as np

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = np.expand_dims(x_train, axis=-1)
x_test = np.expand_dims(x_test, axis=-1)
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
x_train = x_train/255.0
class My_Dnn_Layers(layers.Layer):
    def __init__(self, out):
        super(My_Dnn_Layers, self).__init__()
        self.out = out
        self.w = None

    def build(self, input_shape):
        self.w = self.add_weight(name="w", shape=(int(input_shape[-1]), self.out), dtype="float32")

    def call(self, inputs, *args, **kwargs):
        return tf.matmul(inputs, self.w)


class MyDNN(keras.Model):
    def __init__(self):
        super(MyDNN, self).__init__()
        self.Fatten = layers.Flatten(input_shape=(28, 28))
        self.dense = My_Dnn_Layers(10)

    def call(self, inputs, training=None, mask=None):
        x = self.Fatten(inputs)
        x = self.dense(x)

        return tf.nn.softmax(x)

net = MyDNN()
net.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
net.fit(x_train, y_train, epochs=10)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵药师

嘿嘿嘿

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值