Python广播ValueError乘以numpy矩阵

时间:2016-03-14 09:02:15

标签: python numpy machine-learning list-comprehension

我正在尝试使用我在github上找到的代码使用人工神经网络:https://github.com/stephencwelch/Neural-Networks-Demystified/blob/master/Part%206%20Training.ipynb

然而,虽然矩阵不应该存在维度问题,但我会返回值错误

ValueError: operands could not be broadcast together with shapes (4,1) (51,1)

值51是我的列表的长度,因为我有13个输入变量,我有13个输入节点用于人工神经网络。

我的输入变量的代码如下:

listofbirthweights = [3430., 3815., 3405., 2190.]#, 2990., 3175., 3830., 3447., 3885., 2926., 2632., 4451., 3540., 2720., 4111., 2960., 3240., 3271., 3402., 2364., 3575., 3550., 3997., 3430., 3430., 3827., 2920., 3228., 2807., 4060., 3685.5, 2598., 3345.3, 3005.1000000000004, 3316.9500000000003, 4139.1, 3402L, 3600.4500000000003, 3373.65, 3742.2000000000003, 3061.8, 3572.1000000000004, 2721.6000000000004, 3827.25, 3175.2000000000003, 3515.4, 3997.3500000000004, 2721.6000000000004, 3883.9500000000003, 2523.15]

for weight in range(len(listofbirthweights)):
        listofbirthweights[weight] /= 5000

y = np.array([listofbirthweights]).T

listVariable1 = [0.14639468261405383, 0.97545213377969786, 0.84734727899207973, 1.4484739704326901, 0.16314232728757735, 0.66187166090795935, 1.4376256200580582, 0.29994037390938211, 0.48907780685504304, 0.53109657979711511, 1.2277807441383937, 0.27907030573330116, 1.3695910015926773, 0.14496631493245565, 0.27423132990109989, 0.93782145410422046, 0.14868468846354996, 0.072958383822129144, 1.0420064935722728, 0.54012040073377132, 0.45292701498298288, 0.095267045187387697, 0.50790418911918012, 3.2574574354167067, 0.10911269682418498, 0.16996335861428288, 0.45524586101182357, 0.351405722226031, 0.37124417299819079, 1.4203957243198315, 0.27119225632260868, 1.1113740564160903, 0.19354881514016675, 0.58281757593522832, 0.43372029148679786, 0.098744798454119737, 0.5704041737669534, 0.17066813348854515, 0.30884364780932816, 0.51588822273416624, 0.15695045296832874, 0.048520413305103163, 0.068344316276563011, 0.1315183678181788, 0.3389983553892445, 0.31601229484661936, 0.22713267470170798, 0.12214944241664846, 0.50534165886223625, 0.17544983816074147, 0.11438673225012383]
listVariable2 = [0.10404445113191939, 0.19279221856537584, 0.19863151442801369, 0.15613544736003321, 0.13685881186064319, 0.21790024928306184, 0.2285008959978875, 0.17132273541147319, 0.12186212530933309, 0.11586768312596153, 0.15240676731462721, 0.11983360728747816, 0.20837521980545767, 0.10042039897554125, 0.070516135601742524, 0.21948822954359659, 0.11369528056781744, 0.058745113994697097, 0.25876234910621898, 0.15172764031616054, 0.14198160916163663, 0.078556849795320821, 0.26494990754411307, 0.17906875381180659, 0.07011823589923101, 0.079517491131827003, 0.092000848513905095, 0.15454500379080002, 0.21840477158134278, 0.2294134024515975, 0.085012773523916105, 0.34415042365581427, 0.11357092336082605, 0.18686647426678205, 0.18782663709201797, 0.06178242631156642, 0.16262339377383228, 0.15544278558139304, 0.15065699239514796, 0.23495689198145908, 0.11411254444775469, 0.04023291805330205, 0.059241440093412798, 0.10989999051359937, 0.14767676917896719, 0.18974739389420864, 0.15690022007602542, 0.063365808700030474, 0.12572691445860784, 0.088567961354411431, 0.08900510969553857]
listVariable3 = [0.074863618029391385, 0.095010440494756557, 0.097469695528702424, 0.10502061876774141, 0.114762367888051, 0.09765101230482999, 0.14256688141118506, 0.11169916877052428, 0.061725684295155465, 0.078011539339108621, 0.12426630911150606, 0.081722637644897025, 0.10870057919577722, 0.071181341651149227, 0.033366556530970357, 0.13085602655233133, 0.087481468883883612, 0.048346165493361182, 0.15539661605282443, 0.093560800477644743, 0.069517040270462085, 0.065111072275230275, 0.15458903743493821, 0.1059482501424576, 0.0481638760877472, 0.048330906278235268, 0.052931466865967829, 0.074794265496116702, 0.14584687194117452, 0.11199879427613438, 0.041145862929713707, 0.21696854620842304, 0.074216829958392447, 0.13410960276192421, 0.12484917055142346, 0.043146743378963993, 0.077103864736989131, 0.14028779549247919, 0.0859517900381392, 0.13977865876300227, 0.084691654823737111, 0.033413517733277852, 0.051390066235776209, 0.091198762600111727, 0.07285423352434188, 0.11528025761246247, 0.10931511725529663, 0.037325051526288358, 0.074118578620423786, 0.05424879646640287, 0.068966863864605668]
listVariable4 = [0.055477685335173263, 0.071398299786390312, 0.075681960945485294, 0.082310873359577125, 0.096673494416084721, 0.064353809107946663, 0.10344732784066388, 0.081365542279908859, 0.041943793332494837, 0.05759871124912732, 0.10268311371259752, 0.06802945962346417, 0.065123421579783028, 0.05252602214189226, 0.024544347732606178, 0.091908104759063344, 0.068380919828896464, 0.040694868907437114, 0.10136233199874797, 0.066341114594562975, 0.050140545806246301, 0.05444530377430374, 0.10915896202896243, 0.074957946273929474, 0.035743368515459804, 0.036914234905214387, 0.039978440928878443, 0.044168111360221335, 0.1089615367461696, 0.074837330419096085, 0.03037628233894131, 0.17478209471840711, 0.052912079381077726, 0.11020032805608793, 0.097597719078554496, 0.033504906374757046, 0.050284333684708561, 0.12556885039035326, 0.057593178913847, 0.1000734794663473, 0.065311449747481279, 0.027835691942769813, 0.044684564074273007, 0.075480465826778309, 0.043588295137923672, 0.074959305804439769, 0.078697173326034417, 0.026329913912835925, 0.052895633190573749, 0.039301547158529425, 0.053706561258445161]
listVariable5 = [0.04273587680706812, 0.061039163154928383, 0.065999497852438013, 0.069050240063778939, 0.082139675739585249, 0.05013125099780872, 0.07991309539792911, 0.063554311373857045, 0.031456053125723665, 0.045203647615883975, 0.086355977472030387, 0.059659639469783761, 0.044663922121041144, 0.040631912314692063, 0.021121489729972541, 0.068739629877887967, 0.054642642163422794, 0.034976209330515387, 0.071024708487259447, 0.050148302005558629, 0.041943083969944381, 0.046054672046593172, 0.088478727760740603, 0.056479776744033844, 0.028466242855462256, 0.031702296010483556, 0.033659756739587703, 0.031202264894878894, 0.087270846388028969, 0.054722903489080446, 0.026455602786373857, 0.14413600433318041, 0.039860053285772044, 0.093704954352027914, 0.078308362400716508, 0.028088316432971942, 0.039647671520582808, 0.11161003723337244, 0.043238188393091195, 0.079390343249371373, 0.052714628357895227, 0.023288706582678381, 0.038998452453185208, 0.062566617891961968, 0.030259011636880849, 0.053321988250747118, 0.059234805724335371, 0.021693696431788884, 0.040626425600514944, 0.031451561707650337, 0.042379632136358092]
listVariable6 = [0.03428041013748391, 0.054975545128415189, 0.059156101294205274, 0.059818042267976969, 0.070597436773763955, 0.041532624763040572, 0.064380498308426223, 0.051812408150908956, 0.024880372263535584, 0.037213455210456235, 0.074023964071753434, 0.053125696509212707, 0.03402466222850542, 0.03289717416177116, 0.0190037102380348, 0.053793738796153769, 0.044775403356543307, 0.030602743281610549, 0.052930005783419866, 0.039415966663626088, 0.037057572875412767, 0.039475496304275867, 0.07711112968012486, 0.04411142476891923, 0.023967184293225662, 0.028551529598806955, 0.03013438850353875, 0.024733816647532103, 0.072793273924152704, 0.042040509995572149, 0.024050213201415437, 0.1185388417653342, 0.0310856309055998, 0.081523178864437412, 0.062745815867286003, 0.02465359077795189, 0.034383573640059249, 0.098665627243645554, 0.034988451758209624, 0.066718129236059659, 0.044433913620267572, 0.019586543302390146, 0.034199073057904199, 0.052135548476729798, 0.022973771767903571, 0.041278964789034089, 0.046654110322042003, 0.01965376424630666, 0.032668330916920285, 0.02653403086274558, 0.034110872346882405]
listVariable7 = [2.3978952727983707, 2.0794415416798357, 2.1972245773362196, 2.3025850929940459, 2.0794415416798357, 2.3025850929940459, 2.0794415416798357, 2.3025850929940459, 2.6390573296152584, 2.5649493574615367, 2.1972245773362196, 2.3025850929940459, 2.1972245773362196, 2.3978952727983707, 2.7080502011022101, 2.1972245773362196, 2.3978952727983707, 2.5649493574615367, 2.0794415416798357, 2.5649493574615367, 2.3025850929940459, 2.4849066497880004, 2.0794415416798357, 2.3025850929940459, 2.6390573296152584, 2.3025850929940459, 2.3025850929940459, 2.5649493574615367, 2.0794415416798357, 2.1972245773362196, 2.6390573296152584, 1.791759469228055, 2.4849066497880004, 2.0794415416798357, 1.9459101490553132, 2.5649493574615367, 2.3025850929940459, 1.791759469228055, 2.1972245773362196, 2.1972245773362196, 2.3025850929940459, 2.6390573296152584, 2.3025850929940459, 1.9459101490553132, 2.3025850929940459, 2.0794415416798357, 2.3978952727983707, 2.1972245773362196, 2.3025850929940459, 2.3978952727983707, 2.0794415416798357]
listVariable8 = [2.3111375787927146, 1.6465125691738767, 1.793239968710967, 2.03675590115474, 2.012465676496487, 1.9079220327729067, 1.6686233566181943, 2.1217172303579881, 2.4023778689223705, 2.3828588479304393, 1.9950895487031324, 2.1707082090752499, 1.7826911680515238, 2.3110461410055447, 2.5704359519234159, 1.8147220482305899, 2.3193689810225915, 2.5336557318643358, 1.6553559608320958, 2.3299447384439778, 2.0540400052156826, 2.444244365789213, 1.7524530950459973, 1.8347363463108239, 2.5736904351498953, 2.2054392705313064, 2.1207387911346327, 2.3245932203327997, 1.8603744485774083, 1.645355141986294, 2.4904440778523571, 1.2820148640409508, 2.3658078631374888, 1.8494184204147679, 1.7283796746966074, 2.5091901170611282, 1.9977496022595802, 1.7447013698574527, 1.9996377988600369, 1.8947355154664596, 2.2152144743708315, 2.6183480422246017, 2.2786275315721221, 1.8890554389943486, 2.074762937532209, 1.8609188009226587, 2.2569507636457389, 2.1194537417145991, 2.0835826636629231, 2.2924275908148499, 2.0192251916932378]
listVariable9 = [2.2054164166948498, 1.5593336028893292, 1.7073707518772792, 1.9234446891502106, 1.8866337894969667, 1.7867756550063802, 1.4720080933338222, 1.9722135507741643, 2.3148160101870521, 2.2799344276698319, 1.8555287309061572, 2.0968016155115468, 1.617078253701854, 2.2126416052415467, 2.5405828613213588, 1.6222751261283732, 2.2034626508730177, 2.484475701127892, 1.4069071437991139, 2.1940518993065372, 1.9844208031504922, 2.3718007276990312, 1.5815965639116349, 1.6794916505124431, 2.5137880611982957, 2.1612667020021794, 2.068998129674537, 2.2188979182820185, 1.6775787635668855, 1.4777363652335671, 2.455909195428911, 1.0099423195840642, 2.2561472995961251, 1.7083800897903396, 1.5586979906090466, 2.4639639645645088, 1.9102492218810931, 1.6177417656421471, 1.8817170409172381, 1.7223386454516003, 2.1108707423621413, 2.5786458747202081, 2.228440636424498, 1.776754838338606, 1.9649637153601909, 1.686073212234465, 2.0961678687394758, 2.0827010724707176, 1.9809997599199565, 2.2266082433004621, 1.9234485063486098]
listVariable10 = [2.0897975112105205, 1.4454309108270442, 1.5405762957578966, 1.7547388583934156, 1.6745803481975552, 1.658316134477656, 1.2610631058111803, 1.7876596967549641, 2.2211526544720046, 2.1532498314773179, 1.6323554750912628, 1.9460303180252827, 1.5036221502718519, 2.1073786543634694, 2.4924119004406569, 1.4339968593566095, 2.0540982094342426, 2.397655685534676, 1.1924712572842131, 2.0320295523514087, 1.9112357848600452, 2.2512246817686044, 1.3999303479614555, 1.5007590756296667, 2.4525305080715656, 2.1005729658660472, 2.0147909304969049, 2.1435667842961008, 1.4399156110788967, 1.3068386611360052, 2.3984112857846496, 0.49834146776519572, 2.1284492855731871, 1.4800825207676527, 1.2721960352213291, 2.4003575603732483, 1.8305723360458155, 1.2832774393407513, 1.7825192230299067, 1.5131721172758408, 1.9917604780144951, 2.5041239589734121, 2.1226494896431691, 1.5837378580104067, 1.8757953217802561, 1.5600858279934195, 1.9252874583958293, 2.0646957542636031, 1.8625072564392273, 2.149538786604333, 1.8037990441777916]
listVariable11 = [1.9837646050417153, 1.2613424617733888, 1.260129172334266, 1.5049806267306294, 1.4618977350995359, 1.5254274328434627, 1.0473809082921877, 1.5966131389721168, 2.1308296775076205, 1.9898749047957474, 1.3116860890302715, 1.6861199476145763, 1.3980382043452042, 1.9942315641167947, 2.4053490558815951, 1.2850728897765946, 1.9157101785520374, 2.2608523652704111, 1.0476782931468933, 1.9029919335743579, 1.790397656968501, 2.1207088929234748, 1.0849385414121668, 1.36090573407713, 2.3695469702840852, 2.0031767550374191, 1.9076528029887205, 2.0533128425612741, 1.1848140520480213, 1.1683964184251228, 2.2865925030905636, 0.093931221752249827, 2.0421883706848112, 1.1618503134777527, 1.102366230296022, 2.2897436315265245, 1.7018194974881329, 0.87454529049442908, 1.6789709999878282, 1.2238143026172397, 1.8135369512595645, 2.4250406506328805, 1.9796145325607222, 1.425497372002676, 1.8089587803250091, 1.4414543477439523, 1.7520143654355029, 2.022633297382288, 1.7687126882467876, 2.0597676604962545, 1.718867239356797]
listVariable12 = [1.9282271154063324, 1.1511892465471183, 1.0928577720356314, 1.3183625507623782, 1.3333548351919753, 1.4318875031039773, 0.90617905198106063, 1.5030477476813193, 2.0884051037266103, 1.9085964344036841, 1.0974713665936187, 1.5213912018446361, 1.3425415776017742, 1.9109927778298079, 2.3482736662368002, 1.1778296084209323, 1.8220334571773615, 2.1715903771445371, 0.97403231849660599, 1.8472081629033874, 1.6496249339788183, 2.0279553578915568, 0.8435179672573847, 1.2985158956285483, 2.2941959847442619, 1.9248767762492764, 1.8142679880684507, 1.9878267079418896, 1.0269199848496113, 1.1164863843200918, 2.2043214567556952, 0.016428804738637337, 2.0057060878085724, 0.93155071412636692, 1.0745907343457402, 2.2192631532856621, 1.5990687954968972, 0.74823852255889767, 1.5773132272647519, 1.0614211573245362, 1.6664551794892479, 2.3952295745234777, 1.8894605045520398, 1.3760265321998015, 1.78480140049241, 1.3456315993570127, 1.6592345677714255, 1.9598574330924241, 1.7254844561228693, 1.9973494102587277, 1.6605020956860019]
listVariable13 = [1.8896497279735216, 1.0812580336579187, 0.98754958948515181, 1.1954867755414949, 1.2246462431797318, 1.3497418096385432, 0.80697828400131277, 1.4482911237197831, 2.0649137181850952, 1.8651012191111496, 0.96850617296561214, 1.4332964346761268, 1.3036816730558305, 1.8429048260858731, 2.311606002798432, 1.0786300469226771, 1.7451720250177745, 2.1203230184721189, 0.91145154725178257, 1.8071327841801252, 1.5138112830686485, 1.9413670376332102, 0.64836068143998815, 1.2530537600249412, 2.2308955552081096, 1.85546980434547, 1.7393508178388666, 1.9405892883353428, 0.90493231672865004, 1.0958641300397516, 2.1471886888987846, 0.0027390300930181799, 1.9788692436018942, 0.75213793755676939, 1.0549949469778337, 2.1770094605274082, 1.5092051057464508, 0.70996033359522071, 1.4790518693161869, 0.96392636941413901, 1.5646732725545451, 2.3810384075155335, 1.8242548966294516, 1.3492545060970202, 1.7714307112224172, 1.263322960110296, 1.6064746288171086, 1.8839069061737472, 1.688128481657478, 1.9468706867439955, 1.6078640936959379]

X = np.transpose([listVariable1, listVariable2, listVariable3,listVariable4,listVariable5,listVariable6,listVariable7,listVariable8,listVariable9,listVariable10,listVariable11,listVariable12,listVariable13])

X = X/np.amax(X)

class Neural_Network(object):
    def __init__(self):        
        #Define Hyperparameters
        self.inputLayerSize = 13
        self.outputLayerSize = 1
        self.hiddenLayerSize = 16

        #Weights (parameters)
        self.W1 = np.random.randn(self.inputLayerSize,self.hiddenLayerSize)
        self.W2 = np.random.randn(self.hiddenLayerSize,self.outputLayerSize)

    def forward(self, X):
        #Propogate inputs though network
        self.z2 = np.dot(X, self.W1)
        self.a2 = self.sigmoid(self.z2)
        self.z3 = np.dot(self.a2, self.W2)
        yHat = self.sigmoid(self.z3) 
        return yHat

    def sigmoid(self, z):
        #Apply sigmoid activation function to scalar, vector, or matrix
        return 1/(1+np.exp(-z))

    def sigmoidPrime(self,z):
        #Gradient of sigmoid
        return np.exp(-z)/((1+np.exp(-z))**2)

    def costFunction(self, X, y):
        #Compute cost for given X,y, use weights already stored in class.
        self.yHat = self.forward(X)
        J = 0.5*sum((y-self.yHat)**2)
        return J

    def costFunctionPrime(self, X, y):
        #Compute derivative with respect to W and W2 for a given X and y:
        self.yHat = self.forward(X)

        delta3 = np.multiply(-(y-self.yHat), self.sigmoidPrime(self.z3))
        dJdW2 = np.dot(self.a2.T, delta3)

        delta2 = np.dot(delta3, self.W2.T)*self.sigmoidPrime(self.z2)
        dJdW1 = np.dot(X.T, delta2)  

        return dJdW1, dJdW2

    #Helper Functions for interacting with other classes:
    def getParams(self):
        #Get W1 and W2 unrolled into vector:
        params = np.concatenate((self.W1.ravel(), self.W2.ravel()))
        return params

    def setParams(self, params):
        #Set W1 and W2 using single paramater vector.
        W1_start = 0
        W1_end = self.hiddenLayerSize * self.inputLayerSize
        self.W1 = np.reshape(params[W1_start:W1_end], (self.inputLayerSize , self.hiddenLayerSize))
        W2_end = W1_end + self.hiddenLayerSize*self.outputLayerSize
        self.W2 = np.reshape(params[W1_end:W2_end], (self.hiddenLayerSize, self.outputLayerSize))

    def computeGradients(self, X, y):
        dJdW1, dJdW2 = self.costFunctionPrime(X, y)
        return np.concatenate((dJdW1.ravel(), dJdW2.ravel()))



def computeNumericalGradient(N, X, y):
        paramsInitial = N.getParams()
        numgrad = np.zeros(paramsInitial.shape)
        perturb = np.zeros(paramsInitial.shape)
        e = 1e-4

        for p in range(len(paramsInitial)):
            #Set perturbation vector
            perturb[p] = e
            N.setParams(paramsInitial + perturb)
            loss2 = N.costFunction(X, y)

            N.setParams(paramsInitial - perturb)
            loss1 = N.costFunction(X, y)

            #Compute Numerical Gradient
            numgrad[p] = (loss2 - loss1) / (2*e)

            #Return the value we changed to zero:
            perturb[p] = 0

        #Return Params to original value:
        N.setParams(paramsInitial)

        return numgrad 

from scipy import optimize

class trainer(object):
    def __init__(self, N):
        #Make Local reference to network:
        self.N = N

    def callbackF(self, params):
        self.N.setParams(params)
        self.J.append(self.N.costFunction(self.X, self.y))   

    def costFunctionWrapper(self, params, X, y):
        self.N.setParams(params)
        cost = self.N.costFunction(X, y)
        grad = self.N.computeGradients(X,y)

        return cost, grad

    def train(self, X, y):
        #Make an internal variable for the callback function:
        self.X = X
        self.y = y

        #Make empty list to store costs:
        self.J = []

        params0 = self.N.getParams()

        options = {'maxiter': 200, 'disp' : True}
        _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', \
                                 args=(X, y), options=options, callback=self.callbackF)

        self.N.setParams(_res.x)
        self.optimizationResults = _res

NN = Neural_Network()
T = trainer(NN)
T.train(X,y)
NN.costFunctionPrime(X,y)
NN.forward(X)

如果使用以下变量,代码可以正常工作:

X = np.array(([3.,5.], [5.,1.], [10.,2.]), dtype=float)
X = X/np.amax(X, axis=0)
y = np.array(([75.], [82.], [93.]), dtype=float)    
y = y/100 

将输入参数更改为以下(如网站示例中所示):             self.inputLayerSize = 2             self.outputLayerSize = 1             self.hiddenLayerSize = 3

我推测Python并不喜欢我用我的数据格式化我输入的X和y变量的方式 - 或许它不会将它们识别为numpy数组?

参考的完整错误(我的输入)是:

ValueError                                Traceback (most recent call last)
C:\Users\ENVY14-i7-SPECTRE\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    195             else:
    196                 filename = fname
--> 197             exec compile(scripttext, filename, 'exec') in glob, loc
    198     else:
    199         def execfile(fname, *where):

C:\Users\ENVY14-i7-SPECTRE\Documents\Year 4\AlexMSci\NeuralNetworkMachineLearning2.py in <module>()
    172 NN = Neural_Network()
    173 T = trainer(NN)
--> 174 T.train(X,y)
    175 NN.costFunctionPrime(X,y)
    176 #Xtest = np.array(([4.3,5.], [6.,2.], [12.,6.]), dtype=float)

C:\Users\ENVY14-i7-SPECTRE\Documents\Year 4\AlexMSci\NeuralNetworkMachineLearning2.py in train(self, X, y)
    160         options = {'maxiter': 200, 'disp' : True}
    161         _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', --> 162                                  args=(X, y), options=options, callback=self.callbackF)
    163 
    164         self.N.setParams(_res.x)

C:\Users\ENVY14-i7-SPECTRE\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\_minimize.pyc in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    439         return _minimize_cg(fun, x0, args, jac, callback, **options)
    440     elif meth == 'bfgs':
--> 441         return _minimize_bfgs(fun, x0, args, jac, callback, **options)
    442     elif meth == 'newton-cg':
    443         return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,

C:\Users\ENVY14-i7-SPECTRE\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\optimize.pyc in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
    845     else:
    846         grad_calls, myfprime = wrap_function(fprime, args)
--> 847     gfk = myfprime(x0)
    848     k = 0
    849     N = len(x0)

C:\Users\ENVY14-i7-SPECTRE\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\optimize.pyc in function_wrapper(*wrapper_args)
    287     def function_wrapper(*wrapper_args):
    288         ncalls[0] += 1
--> 289         return function(*(wrapper_args + args))
    290 
    291     return ncalls, function_wrapper

C:\Users\ENVY14-i7-SPECTRE\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\optimize.pyc in derivative(self, x, *args)
     69             return self.jac
     70         else:
---> 71             self(x, *args)
     72             return self.jac
     73 

C:\Users\ENVY14-i7-SPECTRE\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\optimize\optimize.pyc in __call__(self, x, *args)
     61     def __call__(self, x, *args):
     62         self.x = numpy.asarray(x).copy()
---> 63         fg = self.fun(x, *args)
     64         self.jac = fg[1]
     65         return fg[0]

C:\Users\ENVY14-i7-SPECTRE\Documents\Year 4\AlexMSci\NeuralNetworkMachineLearning2.py in costFunctionWrapper(self, params, X, y)
    143     def costFunctionWrapper(self, params, X, y):
    144         self.N.setParams(params)
--> 145         cost = self.N.costFunction(X, y)
    146         grad = self.N.computeGradients(X,y)
    147 

C:\Users\ENVY14-i7-SPECTRE\Documents\Year 4\AlexMSci\NeuralNetworkMachineLearning2.py in costFunction(self, X, y)
     69         #Compute cost for given X,y, use weights already stored in class.
     70         self.yHat = self.forward(X)
---> 71         J = 0.5*sum((y-self.yHat)**2)
     72         return J
     73 

ValueError: operands could not be broadcast together with shapes (4,1) (51,1) 

1 个答案:

答案 0 :(得分:1)

在工作案例中,2个数组是(3,2)(3,1)。它们是可播放的 - 第一个维度匹配,第二个1可以扩展为2

错误发生在

T.train(X,y)

错误堆栈的其余部分可能并不重要,因为它不是您的代码。如果Xy具有正确的形状,我们可以认为它有效。

在错误情况下,我没有看到这两个数组的形状。而且我不打算运行你的代码来弄清楚自己。 :(

我认为你的初始y可以用:

创建
listofbirthweights = np.array([[3430., 3815., 3405., 2190.]]).T/5000.

产生一个(4,1)浮子阵列。

您的X

In [199]: X.shape
Out[199]: (51, 13)

在不了解T.trainer的任何内容的情况下,从错误(4,1) (51,1)开始,它正在使用您的yX列。为什么不是y (51,1)?为什么4?