функция точности, когда метки не имеют горячего кодирования в тензорном потоке NN - PullRequest
0 голосов
/ 01 мая 2018

я делаю двоичную классификацию в тензорном потоке NN без кодирования меток .. все хорошо, кроме функции точности всегда возвращают 1,0 Выход

Эпоха 0 завершена из 10 потерь: 5536.991802096367

Эпоха 1 завершена из 10 потерь: 1777.5951525866985

Эпоха 2 завершена из 10 потерь: 1442.1777643710375

Эпоха 3 завершена из 10 потерь: 1315.4084038436413 , , .

Эпоха 9 завершена из 10 потерь: 968.3492169082165

Точность: 1,0

import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates
import numpy as np
from matplotlib import style
import csv,math,time
from sklearn import preprocessing, cross_validation


import tensorflow as tf
df2=pd.read_csv("Book.csv",encoding="latin-1",index_col=0)
df2['iday'].replace(0,1,inplace=True)
df2['imonth'].replace(0,1,inplace=True)

df2['Datetime'] = pd.to_datetime(dict(year=df2.iyear, month=df2.imonth, day=df2.iday))

print(df2.tail()); 



df=df2[['Datetime','country','longitude','latitude','suicide','attacktype1','targtype1','nkill','nwound','weaptype1','success']]
df.set_index('Datetime',  inplace=True)
df.fillna(value=-99999, inplace=True)  #cleaning data!!!


XX = np.array(df.drop(['success'], 1))   #   
XX = preprocessing.scale(XX)            #dividing dataset into features and label

#df.dropna(inplace=True)                 #dropping those rows which contain nulls
yy = np.array(df['success'])  
yy=yy.reshape(-1,1)
X_train, X_test, y_train, y_test = cross_validation.train_test_split(XX, yy,random_state=2) #training


n_nodes_hl1 = 100
n_nodes_hl2 = 100
n_nodes_hl3 = 100
n_input=X_train.shape[1]
print(X_train.shape)

print(yy.shape)
n_classes=y_train.shape[1]
batch_size = 100

x = tf.placeholder('float', [None, n_input])
y = tf.placeholder('float')

def neural_network_model(data):
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([n_input, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes])),}


    l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']

    return output

def train_neural_network(x):
    prediction = neural_network_model(x)
    # OLD VERSION:
    #cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
    # NEW:
    cost = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=prediction, labels=y) )
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10
    with tf.Session() as sess:
        # OLD:
        #sess.run(tf.initialize_all_variables())
        # NEW:
        sess.run(tf.global_variables_initializer())

        for epoch in range(hm_epochs):
            total_batch = int(len(X_train) / batch_size)
            print(total_batch)
            x_batches = np.array_split(X_train, total_batch)
            y_batches = np.array_split(y_train, total_batch)
            epoch_loss = 0
            for i in range(total_batch):
                epoch_x, epoch_y =x_batches[i], y_batches[i]
                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: 
           epoch_y})

                epoch_loss += c

            print('Epoch', epoch, 'completed out 
            of',hm_epochs,'loss:',epoch_loss)

        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))


        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy:',accuracy.eval({x:X_test, y:y_test}))

train_neural_network(x)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...