首页 > 解决方案 > 为什么这个列表:[[-0.23684 -2.54357006 1.06844643]] 返回为无类型?查看代码输出

问题描述

当执行下面列出的输出命令 (1.) 时,您可以看到有一个列表输出:[[0.1357893 0.13558951 0.13562417]],当命令 (5.) 询问此输出的类型时,它输出为 class <'None键入'>。所以问题是为什么这个非空列表被称为无类型。你可以看到没有错误输出。

您还可以确认输出 (2.) 是否是数组列表,这就是它列出类型的原因。

1.Network.feed forward ([1,5])

2.print(网络权重)

3.print(Network.biases)

4.print(类型(网络权重))

5.print(类型(网络。前馈([1,5])))

6.print(长度(Network.weights))

它给出了如下所述的输出

1.[[0.1357893 0.13558951 0.13562417]]

2. [array([[ 1.17769878, 1.09506853], [ 1.13799858, 2.71622618], [-0.35448734, 1.75165491]]), array([[-1.56395246, 0.83951109, -0.27785569]])] 3.[array([[0.02232141], [0.65477376], [0.19102921]]), array([[-0.85011921]])]

4.<class 'list'> 为什么会这样输出?[[0.13618594 0.1355904 0.13578948]]

5.<class 'NoneType'>

6.2

进程以退出代码 0 结束

这是相关代码:

import numpy as np

class Network(object):

    def __init__(self, sizes):
        """The list ``sizes`` contains the number of neurons in the
        respective layers of the network.  """
        self.num_layers = len(sizes)
        self.sizes = sizes
        self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
        self.weights = [np.random.randn(y, x)
                        for x, y in zip(sizes[:-1], sizes[1:])]

    def feedforward(self, a):
        """Return the output of the network if ``a`` is input."""
        for b, w in zip(self.biases, self.weights):
            a = np.dot(w, a)+b
        print(a)


Network = Network([2,3,1])
Network.feedforward([1,5])
print(Network.weights)
print(Network.biases)
print(type(Network.weights))
print(type(Network.feedforward([1,5])))
print(len(Network.weights))

标签: pythonnumpyoop

解决方案


Network.feedforward([1,5])不返回任何东西。

您需要在函数return末尾添加一条语句feedforward,因为feedforward编辑您给它但返回的数组None


推荐阅读