Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Tensorflow和keras:自定义层和模型存在问题。与(无、H、W、C)tensor相关的拟合_Python 3.x_Numpy_Keras Layer_Tensorflow2.0 - Fatal编程技术网

Python 3.x Tensorflow和keras:自定义层和模型存在问题。与(无、H、W、C)tensor相关的拟合

Python 3.x Tensorflow和keras:自定义层和模型存在问题。与(无、H、W、C)tensor相关的拟合,python-3.x,numpy,keras-layer,tensorflow2.0,Python 3.x,Numpy,Keras Layer,Tensorflow2.0,早上好! 我已经搜索了已经提出的问题,但没有找到任何关于我的问题,但我错过了一个相关的问题,请张贴链接,我会看看那里 首先:我使用的是python 3.7.3和tensorflow 2.0 我想写一个自定义展平层,其中通道索引是展平数据上变化最慢的索引。可能有更好的方法可以做到这一点,但我的方法包括通过以下方式获得通道数、高度和宽度: def call(self,input): np_input = np.array([input]) input_shape =

早上好! 我已经搜索了已经提出的问题,但没有找到任何关于我的问题,但我错过了一个相关的问题,请张贴链接,我会看看那里

首先:我使用的是python 3.7.3和tensorflow 2.0

我想写一个自定义展平层,其中通道索引是展平数据上变化最慢的索引。可能有更好的方法可以做到这一点,但我的方法包括通过以下方式获得通道数、高度和宽度:

def call(self,input):
        np_input = np.array([input]) 
        input_shape = np.shape(np_input)
        numChan = input_shape_sq[-1]
        numBatch = input_shape[0]
        width = input_shape[1]
        height = input_shape[2]
然后手动循环通道并展平每个图像,然后将它们附加到单个向量

当我通过“手动”发送图像时,这很好,但当我调用model.fit时,它似乎首先通过形状张量(无,28,28,1)发送(我将使用MNIST数据,因此是28 x 28),这会抛出一个错误“Indexer:元组索引超出范围”

所以我的问题是如何处理这个问题,更一般地说,当通过操纵numpy数组来创建自定义层时,您需要考虑什么

更明确的代码: 我举了一个(我认为)代码中断的最小示例:

import tensorflow as tf
import numpy as np

class simple(tf.keras.layers.Layer):
    def __init__(self):
        super(simple, self).__init__()

    def build(self,input_shape):
        super(simple,self).build(input_shape)
        pass

    def call(self,input):
        np_input = np.array([input]) 
        input_shape = np.shape(np_input)
        numChan = input_shape[-1]
        numBatch = input_shape[0]
        width = input_shape[1]
        height = input_shape[2]
        return input

(我还没有使用tf2,因此此注释基于TF1)。您必须使用这些numpy函数的tensorflow版本,可能是先置换,然后是展平。请务必记住,tensorflow图不像python脚本那样按顺序执行。相反,python脚本定义了图形,并由一些底层c库高效运行。因此,您不能只是将一些numpy指令插入到层中,所有指令都必须是tensorflow指令,以便它们构成图形的一部分。听起来很合理,尽管我更希望能够在不破坏.fit()dunction的情况下与numpy进行转换。我还没有找到任何关于tf2的tensorflow图的好资源,我只看到在谈论tf1时提到的图。你有什么建议我可以在哪里查阅吗?此外,是否有已实现的numpy函数的列表,或者您建议使用“tf.numpy_function()”?你知道为什么model.fit首先通过模型发送一个形状张量(None,H,W,C)?看了一会儿后,我认为该做什么是可能的,但我需要使用切片分配,如numpy:finalSensor[I,a:b]=其他张量。问题是,我不能把我的头绕在散布上,以及如何对轴的子集进行赋值。文档不是很有用。关于这个有什么提示吗?图形是包含所有连接在一起的张量的支持对象。在keras之前的早期,你会通过将张量连接在一起创建一个图(如神经网络),然后通过输入和输出来运行该网络。Keras在某种程度上抽象了这一点,但我相信在其背后,一切都是一样的。图中的所有操作都是用cuda加速等编译的C代码。这里有一些tensorflow数学操作
import tensorflow as tf
from tensorflow import keras
import simpleLayer

mnist = keras.datasets.mnist

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

train_labels=keras.utils.to_categorical(train_labels)
test_labels=keras.utils.to_categorical(test_labels)

train_images=train_images.reshape(len(train_images),28,28,1)
test_images=test_images.reshape(len(test_images),28,28,1)


train_images = train_images/255.0
test_images = test_images/255.0


model = tf.keras.Sequential([
    simpleLayer.simple(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10)
])

test = model(train_images[0:10])
model.summary()

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

model.fit(train_images,train_labels,epochs=1,verbose=2)