Tensorflow输出NaN用于交叉熵

时间:2018-07-10 17:16:02

标签: python tensorflow neural-network

import pandas as pd
import numpy as np
import tensorflow as tf

dataframe = pd.read_csv("data.csv")
dataframe = dataframe.drop(["id"], axis = 1)

train = dataframe[1:250]
test = dataframe[251:569]

dataY = train["diagnosis"]
for key,value in dataY.iteritems():
    if value == "M":
        dataY[key] = 1
    if value == "B":
        dataY[key] = 0
dataY = np.asarray(dataY)
dataX = np.asarray(train.drop(["diagnosis"], axis = 1))

trainRows = len(train)
trainColumns = len(dataframe.columns)-1

inputX = tf.placeholder(tf.float32, [trainRows, trainColumns])
inputY = tf.placeholder(tf.float32,  [trainRows])

W = tf.Variable(tf.zeros([trainColumns, trainRows]))
b = tf.Variable(tf.zeros([trainRows]))

Y_compare = tf.nn.softmax(tf.matmul(inputX, W)+b)

cross_entropy = -tf.reduce_sum(inputY * tf.log(Y_compare))

optimizer = tf.train.GradientDescentOptimizer(.001)
trainer = optimizer.minimize(cross_entropy)

init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)
for step in range(1000):
    sess.run(trainer, feed_dict={inputX: dataX, inputY: dataY})
    print(sess.run(cross_entropy, feed_dict={inputX: dataX, inputY: dataY}))
sess.close()

当我运行这段代码时,它只为每一步打印nan。我尝试更改错误函数,但没有任何区别。我很确定问题出在错误函数或优化器上。有什么问题的想法吗?

有关乳腺癌诊断的数据:

这是输入X:

[[  20.57    17.77   132.9   1326.   ]
 [  19.69    21.25   130.    1203.   ]
 [  11.42    20.38    77.58   386.1  ]
 [  20.29    14.34   135.1   1297.   ]
 [  12.45    15.7     82.57   477.1  ]
 [  18.25    19.98   119.6   1040.   ]
 [  13.71    20.83    90.2    577.9  ]
 [  13.      21.82    87.5    519.8  ]
 [  12.46    24.04    83.97   475.9  ]
 [  16.02    23.24   102.7    797.8  ]
 [  15.78    17.89   103.6    781.   ]
 [  19.17    24.8    132.4   1123.   ]
 [  15.85    23.95   103.7    782.7  ]
 [  13.73    22.61    93.6    578.3  ]
 [  14.54    27.54    96.73   658.8  ]
 [  14.68    20.13    94.74   684.5  ]
 [  16.13    20.68   108.1    798.8  ]
 [  19.81    22.15   130.    1260.   ]
 [  13.54    14.36    87.46   566.3  ]
 [  13.08    15.71    85.63   520.   ]
 [   9.504   12.44    60.34   273.9  ]
 [  15.34    14.26   102.5    704.4  ]
 [  21.16    23.04   137.2   1404.   ]
 [  16.65    21.38   110.     904.6  ]
 [  17.14    16.4    116.     912.7  ]
 [  14.58    21.53    97.41   644.8  ]
 [  18.61    20.25   122.1   1094.   ]
 [  15.3     25.27   102.4    732.4  ]
 [  17.57    15.05   115.     955.1  ]
 [  18.63    25.11   124.8   1088.   ]
 [  11.84    18.7     77.93   440.6  ]
 [  17.02    23.98   112.8    899.3  ]
 [  19.27    26.47   127.9   1162.   ]
 [  16.13    17.88   107.     807.2  ]
 [  16.74    21.59   110.1    869.5  ]
 [  14.25    21.72    93.63   633.   ]
 [  13.03    18.42    82.61   523.8  ]
 [  14.99    25.2     95.54   698.8  ]
 [  13.48    20.82    88.4    559.2  ]
 [  13.44    21.58    86.18   563.   ]
 [  10.95    21.35    71.9    371.1  ]
 [  19.07    24.81   128.3   1104.   ]
 [  13.28    20.28    87.32   545.2  ]
 [  13.17    21.81    85.42   531.5  ]
 [  18.65    17.6    123.7   1076.   ]
 [   8.196   16.84    51.71   201.9  ]
 [  13.17    18.66    85.98   534.6  ]
 [  12.05    14.63    78.04   449.3  ]
 [  13.49    22.3     86.91   561.   ]
 [  11.76    21.6     74.72   427.9  ]
 [  13.64    16.34    87.21   571.8  ]
 [  11.94    18.24    75.71   437.6  ]
 [  18.22    18.7    120.3   1033.   ]
 [  15.1     22.02    97.26   712.8  ]
 [  11.52    18.75    73.34   409.   ]
 [  19.21    18.57   125.5   1152.   ]
 [  14.71    21.59    95.55   656.9  ]
 [  13.05    19.31    82.61   527.2  ]
 [   8.618   11.79    54.34   224.5  ]
 [  10.17    14.88    64.55   311.9  ]
 [   8.598   20.98    54.66   221.8  ]
 [  14.25    22.15    96.42   645.7  ]
 [   9.173   13.86    59.2    260.9  ]
 [  12.68    23.84    82.69   499.   ]
 [  14.78    23.94    97.4    668.3  ]
 [   9.465   21.01    60.11   269.4  ]
 [  11.31    19.04    71.8    394.1  ]
 [   9.029   17.33    58.79   250.5  ]
 [  12.78    16.49    81.37   502.5  ]
 [  18.94    21.31   123.6   1130.   ]
 [   8.888   14.64    58.79   244.   ]
 [  17.2     24.52   114.2    929.4  ]
 [  13.8     15.79    90.43   584.1  ]
 [  12.31    16.52    79.19   470.9  ]
 [  16.07    19.65   104.1    817.7  ]
 [  13.53    10.94    87.91   559.2  ]
 [  18.05    16.15   120.2   1006.   ]
 [  20.18    23.97   143.7   1245.   ]
 [  12.86    18.      83.19   506.3  ]
 [  11.45    20.97    73.81   401.5  ]
 [  13.34    15.86    86.49   520.   ]
 [  25.22    24.91   171.5   1878.   ]
 [  19.1     26.29   129.1   1132.   ]
 [  12.      15.65    76.95   443.3  ]
 [  18.46    18.52   121.1   1075.   ]
 [  14.48    21.46    94.25   648.2  ]
 [  19.02    24.59   122.    1076.   ]
 [  12.36    21.8     79.78   466.1  ]
 [  14.64    15.24    95.77   651.9  ]
 [  14.62    24.02    94.57   662.7  ]
 [  15.37    22.76   100.2    728.2  ]
 [  13.27    14.76    84.74   551.7  ]
 [  13.45    18.3     86.6    555.1  ]
 [  15.06    19.83   100.3    705.6  ]
 [  20.26    23.03   132.4   1264.   ]
 [  12.18    17.84    77.79   451.1  ]
 [   9.787   19.94    62.11   294.5  ]
 [  11.6     12.84    74.34   412.6  ]
 [  14.42    19.77    94.48   642.5  ]
 [  13.61    24.98    88.05   582.7  ]
 [   6.981   13.43    43.79   143.5  ]
 [  12.18    20.52    77.22   458.7  ]
 [   9.876   19.4     63.95   298.3  ]
 [  10.49    19.29    67.41   336.1  ]
 [  13.11    15.56    87.21   530.2  ]
 [  11.64    18.33    75.17   412.5  ]
 [  12.36    18.54    79.01   466.7  ]
 [  22.27    19.67   152.8   1509.   ]
 [  11.34    21.26    72.48   396.5  ]
 [   9.777   16.99    62.5    290.2  ]
 [  12.63    20.76    82.15   480.4  ]
 [  14.26    19.65    97.83   629.9  ]
 [  10.51    20.19    68.64   334.2  ]
 [   8.726   15.83    55.84   230.9  ]
 [  11.93    21.53    76.53   438.6  ]
 [   8.95    15.76    58.74   245.2  ]
 [  14.87    16.67    98.64   682.5  ]
 [  15.78    22.91   105.7    782.6  ]
 [  17.95    20.01   114.2    982.   ]
 [  11.41    10.82    73.34   403.3  ]
 [  18.66    17.12   121.4   1077.   ]
 [  24.25    20.2    166.2   1761.   ]
 [  14.5     10.89    94.28   640.7  ]
 [  13.37    16.39    86.1    553.5  ]
 [  13.85    17.21    88.44   588.7  ]
 [  13.61    24.69    87.76   572.6  ]
 [  19.      18.91   123.4   1138.   ]
 [  15.1     16.39    99.58   674.5  ]
 [  19.79    25.12   130.4   1192.   ]
 [  12.19    13.29    79.08   455.8  ]
 [  15.46    19.48   101.7    748.9  ]
 [  16.16    21.54   106.2    809.8  ]
 [  15.71    13.93   102.     761.7  ]
 [  18.45    21.91   120.2   1075.   ]
 [  12.77    22.47    81.72   506.3  ]
 [  11.71    16.67    74.72   423.6  ]
 [  11.43    15.39    73.06   399.8  ]
 [  14.95    17.57    96.85   678.1  ]
 [  11.28    13.39    73.     384.8  ]
 [   9.738   11.97    61.24   288.5  ]
 [  16.11    18.05   105.1    813.   ]
 [  11.43    17.31    73.66   398.   ]
 [  12.9     15.92    83.74   512.2  ]
 [  10.75    14.97    68.26   355.3  ]
 [  11.9     14.65    78.11   432.8  ]
 [  11.8     16.58    78.99   432.   ]
 [  14.95    18.77    97.84   689.5  ]
 [  14.44    15.18    93.97   640.1  ]
 [  13.74    17.91    88.12   585.   ]
 [  13.      20.78    83.51   519.4  ]
 [   8.219   20.7     53.27   203.9  ]
 [   9.731   15.34    63.78   300.2  ]
 [  11.15    13.08    70.87   381.9  ]
 [  13.15    15.34    85.31   538.9  ]
 [  12.25    17.94    78.27   460.3  ]
 [  17.68    20.74   117.4    963.7  ]
 [  16.84    19.46   108.4    880.2  ]
 [  12.06    12.74    76.84   448.6  ]
 [  10.9     12.96    68.69   366.8  ]
 [  11.75    20.18    76.1    419.8  ]
 [  19.19    15.94   126.3   1157.   ]
 [  19.59    18.15   130.7   1214.   ]
 [  12.34    22.22    79.85   464.5  ]
 [  23.27    22.04   152.1   1686.   ]
 [  14.97    19.76    95.5    690.2  ]
 [  10.8      9.71    68.77   357.6  ]
 [  16.78    18.8    109.3    886.3  ]
 [  17.47    24.68   116.1    984.6  ]
 [  14.97    16.95    96.22   685.9  ]
 [  12.32    12.39    78.85   464.1  ]
 [  13.43    19.63    85.84   565.4  ]
 [  15.46    11.89   102.5    736.9  ]
 [  11.08    14.71    70.21   372.7  ]
 [  10.66    15.15    67.49   349.6  ]
 [   8.671   14.45    54.42   227.2  ]
 [   9.904   18.06    64.6    302.4  ]
 [  16.46    20.11   109.3    832.9  ]
 [  13.01    22.22    82.01   526.4  ]
 [  12.81    13.06    81.29   508.8  ]
 [  27.22    21.87   182.1   2250.   ]
 [  21.09    26.57   142.7   1311.   ]
 [  15.7     20.31   101.2    766.6  ]
 [  11.41    14.92    73.53   402.   ]
 [  15.28    22.41    98.92   710.6  ]
 [  10.08    15.11    63.76   317.5  ]
 [  18.31    18.58   118.6   1041.   ]
 [  11.71    17.19    74.68   420.3  ]
 [  11.81    17.39    75.27   428.9  ]
 [  12.3     15.9     78.83   463.7  ]
 [  14.22    23.12    94.37   609.9  ]
 [  12.77    21.41    82.02   507.4  ]
 [   9.72    18.22    60.73   288.1  ]
 [  12.34    26.86    81.15   477.4  ]
 [  14.86    23.21   100.4    671.4  ]
 [  12.91    16.33    82.53   516.4  ]
 [  13.77    22.29    90.63   588.9  ]
 [  18.08    21.84   117.4   1024.   ]
 [  19.18    22.49   127.5   1148.   ]
 [  14.45    20.22    94.49   642.7  ]
 [  12.23    19.56    78.54   461.   ]
 [  17.54    19.32   115.1    951.6  ]
 [  23.29    26.67   158.9   1685.   ]
 [  13.81    23.75    91.56   597.8  ]
 [  12.47    18.6     81.09   481.9  ]
 [  15.12    16.68    98.78   716.6  ]
 [   9.876   17.27    62.92   295.4  ]
 [  17.01    20.26   109.7    904.3  ]
 [  13.11    22.54    87.02   529.4  ]
 [  15.27    12.91    98.17   725.5  ]
 [  20.58    22.14   134.7   1290.   ]
 [  11.84    18.94    75.51   428.   ]
 [  28.11    18.47   188.5   2499.   ]
 [  17.42    25.56   114.5    948.   ]
 [  14.19    23.81    92.87   610.7  ]
 [  13.86    16.93    90.96   578.9  ]
 [  11.89    18.35    77.32   432.2  ]
 [  10.2     17.48    65.05   321.2  ]
 [  19.8     21.56   129.7   1230.   ]
 [  19.53    32.47   128.    1223.   ]
 [  13.65    13.16    87.88   568.9  ]
 [  13.56    13.9     88.59   561.3  ]
 [  10.18    17.53    65.12   313.1  ]
 [  15.75    20.25   102.6    761.3  ]
 [  13.27    17.02    84.55   546.4  ]
 [  14.34    13.47    92.51   641.2  ]
 [  10.44    15.46    66.62   329.6  ]
 [  15.      15.51    97.45   684.5  ]
 [  12.62    23.97    81.35   496.4  ]
 [  12.83    22.33    85.26   503.2  ]
 [  17.05    19.08   113.4    895.   ]
 [  11.32    27.08    71.76   395.7  ]
 [  11.22    33.81    70.79   386.8  ]
 [  20.51    27.81   134.4   1319.   ]
 [   9.567   15.91    60.21   279.6  ]
 [  14.03    21.25    89.79   603.4  ]
 [  23.21    26.97   153.5   1670.   ]
 [  20.48    21.46   132.5   1306.   ]
 [  14.22    27.85    92.55   623.9  ]
 [  17.46    39.28   113.4    920.6  ]
 [  13.64    15.6     87.38   575.3  ]
 [  12.42    15.04    78.61   476.5  ]
 [  11.3     18.19    73.93   389.4  ]
 [  13.75    23.77    88.54   590.   ]
 [  19.4     23.5    129.1   1155.   ]
 [  10.48    19.86    66.72   337.7  ]
 [  13.2     17.43    84.13   541.6  ]
 [  12.89    14.11    84.95   512.2  ]
 [  10.65    25.22    68.01   347.   ]
 [  11.52    14.93    73.87   406.3  ]]

这是输入Y:

[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 1 0 1 1 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 1 0
 1 0 1 1 0 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 0
 0 0 0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 1 0 1 1 0 1 1 0 0 1 0 0 1 0 0 0 0 1 0 0
 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 1 0 0 1 1 1 0 1 0
 1 0 0 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 1 0 0 1 1 0 0 0
 1 0 0 0 0 0 1 1 0 0 1 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0]

0 个答案:

没有答案