コード例 #1
0
    def test_run(self):
        # Test correctness of run method
        network = NeuralNetwork(3, 2, 1, 0.5)
        network.weights_input_to_hidden = test_w_i_h.copy()
        network.weights_hidden_to_output = test_w_h_o.copy()

        self.assertTrue(np.allclose(network.run(inputs), 0.09998924))
コード例 #2
0
    def __init__(self, host, port, model_path):

        self.server_socket = socket.socket()
        self.server_socket.bind((host, port))
        self.server_socket.listen(0)

        # accept a single connection
        self.connection = self.server_socket.accept()[0].makefile('rb')

        self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.client_socket.connect(('192.168.0.115', 1234))  #pi

        # load trained neural network
        self.nn = NeuralNetwork()
        self.nn.load_modelKeras("model_test.h5")

        self.h1 = 5.5  #stop sign - measure manually

        #self.stop_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +"C:/Users/My\ PC/Anaconda3/envs/tf/Lib/site-packages/cv2/data/stop_sign.xml")
        #self.stop_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +"D:/Downloads/self-driving-car2/neural networks/stop_sign.xml")
        self.stop_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
                                                  "stop_sign.xml")

        self.d_stop_light_thresh = 70
        self.d_stop_sign = self.d_stop_light_thresh

        self.stop_start = 0  # start time when stop at the stop sign
        self.stop_finish = 0
        self.stop_time = 0
        self.drive_time_after_stop = 0

        self.alpha = 8.0 * math.pi / 180  # degree measured manually
        self.v0 = 119.865631204  # from camera matrix
        self.ay = 332.262498472  # from camera matrix
コード例 #3
0
    def __init__(self, host_str, model_path):

        # load trained neural network
        self.nn = NeuralNetwork()
        self.nn.load_model(model_path)

        self.car_ctrl = sock_car_control()
コード例 #4
0
ファイル: qii.py プロジェクト: hovinh/IRIS_Transparency
def test_case_2():
    import time
    dataset = IRISDataset()
    X_test, X_train, y_test, y_train = dataset.split_to_train_test(seed=10)
    model = NeuralNetwork()
    model.create_graph()
    hl_1, hl_2, hl_3, prediction = model.predict(X_train)
    numb_features = 8
    feature_list = [str(i) for i in range(1, numb_features + 1)]
    influencer = QII(hl_1, feature_list)
    data_point = hl_1[0]
    with tf.Session(graph=model.graph) as sess:
        saver = tf.train.Saver()
        saver.restore(sess, model.CHECK_POINT)
        start_time = time.time()
        print('Approximation with Random Sampling')
        shapley_val = influencer.banzhaf(data_point,
                                         model.predict_hl_1,
                                         sess,
                                         is_exhaustive=False)
        print(shapley_val)
        print("--- %s seconds ---" % (time.time() - start_time))

        start_time = time.time()
        print('Correct value with Exhaustive Computation')
        shapley_val = influencer.banzhaf(data_point,
                                         model.predict_hl_1,
                                         sess,
                                         is_exhaustive=True)
        print(shapley_val)
        print("--- %s seconds ---" % (time.time() - start_time))
コード例 #5
0
class CollectTrainingData(object):
    def __init__(self, client, steer):

        self.client = client
        self.steer = steer

        # 정지선 검출 모델 선언
        self.stopline = StopLine.Stop()

        # YOLO 모델 선언
        self.dect = Object.Object_Detection(self.steer)

        # model create
        # 주행 모델 선언
        self.model = NeuralNetwork()
        self.model.load_model(path='model_data/video_model_1.h5')

    def collect(self):

        print("Start video stream")

        stream_bytes = b' '

        while True:
            stream_bytes += self.client.recv(1024)
            first = stream_bytes.find(b'\xff\xd8')
            last = stream_bytes.find(b'\xff\xd9')

            if first != -1 and last != -1:
                try:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]

                    image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                         cv2.IMREAD_GRAYSCALE)
                    rgb = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                       cv2.IMREAD_COLOR)
                    rgb2 = rgb.copy()
                    roi = image[120:240, :]
                    roi2 = rgb2[120:240, :]  #for line roi

                    cv2.imshow('Origin', rgb)
                    cv2.imshow('GRAY', image)
                    cv2.imshow('roi', roi)

                    # reshape the roi image into a vector
                    image_array = np.reshape(roi, (-1, 120, 320, 1))

                    # neural network makes prediction
                    self.steer.Set_Line(self.model.predict(image_array))
                    self.steer.Set_Stopline(self.stopline.GetStopLine(roi2))
                    self.dect.Detection(rgb)

                    # steer의 주행함수 실행
                    self.steer.Control()
                except:
                    continue

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
コード例 #6
0
ファイル: qii.py プロジェクト: hovinh/IRIS_Transparency
    def use_QII_with_model(X_train):
        model = NeuralNetwork()
        model.create_graph()
        hl_1, hl_2, hl_3, prediction = model.predict(X_train)
        numb_features = 4  # hidden layer 3 has 4 units
        feature_list = [str(i) for i in range(1, numb_features + 1)]
        influencer = QII(hl_3, feature_list)

        # value of one data points we want to observe
        data_point = hl_3[0]

        with tf.Session(graph=model.graph) as sess:
            saver = tf.train.Saver()
            saver.restore(sess, model.CHECK_POINT)
            print('Correct value with Exhaustive Computation')
            banzhaf_val = influencer.banzhaf(data_point,
                                             model.predict_hl_3,
                                             sess,
                                             is_exhaustive=True)
            print(banzhaf_val)

            influence_scores = np.array([banzhaf_val[i] for i in feature_list])
            influence_scores = np.expand_dims(influence_scores, axis=0)
            weight_name_list = ['w1', 'w2', 'w3']
            influence_scores = influencer.mapback(model, X_train,
                                                  influence_scores,
                                                  weight_name_list)
            influencer.plot_influence_score(influence_scores, feature_list)
コード例 #7
0
ファイル: qii.py プロジェクト: hovinh/IRIS_Transparency
    def use_QII_with_model(X_train):
        model = NeuralNetwork()
        model.create_graph()
        hl_1, hl_2, hl_3, prediction = model.predict(X_train)
        numb_features = 4  # hidden layer 3 has 4 units
        feature_list = [str(i) for i in range(1, numb_features + 1)]
        influencer = QII(hl_3, feature_list)

        # value of one data points we want to observe
        data_point = hl_3[0]

        assignment_list = ['normalized', 'ranking', 'nochange']
        propagate_list = [
            'transpose_mul', 'inverse_mul', 'softmax_dist', 'shift_dist',
            'shift_softmax_dist'
        ]

        with tf.Session(graph=model.graph) as sess:
            saver = tf.train.Saver()
            saver.restore(sess, model.CHECK_POINT)
            for assignment in assignment_list:
                for propagate in propagate_list:
                    banzhaf_val = influencer.banzhaf(data_point,
                                                     model.predict_hl_3,
                                                     sess,
                                                     is_exhaustive=False)
                    influence_scores = np.array(
                        [banzhaf_val[i] for i in feature_list])
                    influence_scores = np.expand_dims(influence_scores, axis=0)
                    weight_name_list = ['w1', 'w2', 'w3']
                    influence_scores = influencer.mapback(
                        model, X_train, influence_scores, weight_name_list,
                        assignment, propagate)
                    influencer.plot_influence_score(influence_scores,
                                                    feature_list)
コード例 #8
0
def worker(args, STD_NOISE, STATE_DIM, ACTION_DIM, params_queue, output_queue,
           elite_queue):
    # Function execute by each worker: get the agent' NN, sample noise and evaluate the agent adding the noise. Then return the seed and the rewards to the central unit

    env = gym.make(args.env)
    # env = CyclicMDP()
    actor = NeuralNetwork(STATE_DIM, ACTION_DIM.shape[0], args.hidden_size)
    while True:
        # get the new actor's params
        act_params = params_queue.get()
        if act_params != None:
            # load the actor params
            actor.load_state_dict(act_params)

            # get a random seed
            seed = np.random.randint(1e6)
            # set the new seed
            np.random.seed(seed)

            noise = sample_noise(actor)

            pos_rew = evaluate_noisy_net(STD_NOISE, noise, actor, env,
                                         elite_queue)
            # Mirrored sampling
            neg_rew = evaluate_noisy_net(STD_NOISE, -noise, actor, env,
                                         elite_queue)

            output_queue.put([[pos_rew, neg_rew], seed])
        else:
            break
コード例 #9
0
ファイル: main.py プロジェクト: roblburris/kidney-ml
def main():
    # load dataset
    adata = load_data('./data/')
    train_adata, test_adata = train_test(adata, 0.8)

    dataset = KidneyDataset('./data/')

    train_set, test_set = random_split(dataset, [math.floor(len(dataset) * 0.8), len(dataset) - math.floor(len(dataset) * 0.8)])

    # not sure about batch sizes, need to figure this out more
    train_dl = DataLoader(train_set, batch_size=64)
    test_dl = DataLoader(test_set, batch_size=64)

    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    print(f'Using {device} device')

    model = NeuralNetwork()
    model = model.to(device)
    wandb.watch(model)
    loss_fn = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)

    for i in range(4):
        print(f"Epoch {i+1}\n-------------------------------")
        train(train_dl, model, loss_fn, optimizer)

    test(test_dl, model)
コード例 #10
0
    def __init__(self, host, port, _host, _port, model_path):

        # load trained neural network
        self.nn = NeuralNetwork(model_path)
        self.cap = cv2.VideoCapture(0)

        self.rc_car = RCControl(_h, _p)
コード例 #11
0
class RCDriverNNOnly(object):
    def __init__(self, host, port, serial_port, model_path):

        self.server_socket = socket.socket()
        self.server_socket.bind((host, port))
        self.server_socket.listen(0)

        # accept a single connection
        self.connection = self.server_socket.accept()[0].makefile('rb')

        # connect to a seral port
        self.ser = serial.Serial(serial_port, 115200, timeout=1)

        # load trained neural network
        self.nn = NeuralNetwork()
        self.nn.load_model(model_path)

        self.rc_car = RCControl(serial_port)

    def drive(self):
        stream_bytes = b' '
        try:
            # stream video frames one by one
            while True:
                stream_bytes += self.connection.read(1024)
                first = stream_bytes.find(b'\xff\xd8')
                last = stream_bytes.find(b'\xff\xd9')

                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]
                    gray = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                        cv2.IMREAD_GRAYSCALE)
                    image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                         cv2.IMREAD_COLOR)

                    # lower half of the image
                    height, width = gray.shape
                    roi = gray[int(height / 2):height, :]

                    cv2.imshow('image', image)
                    # cv2.imshow('mlp_image', roi)

                    # reshape image
                    image_array = roi.reshape(1,
                                              int(height / 2) * width).astype(
                                                  np.float32)

                    # neural network makes prediction
                    prediction = self.nn.predict(image_array)
                    self.rc_car.steer(prediction)

                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        print("car stopped")
                        self.rc_car.stop()
                        break
        finally:
            cv2.destroyAllWindows()
            self.connection.close()
            self.server_socket.close()
コード例 #12
0
ファイル: qii.py プロジェクト: hovinh/IRIS_Transparency
 def train_model(X_test, X_train, y_test, y_train):
     print_steps = 50
     epoch = 100
     model = NeuralNetwork()
     model.initialize_variables()
     model.create_graph()
     model.train(X_train, y_train, print_steps, epoch)
     model.test(X_test, y_test)
     return model
コード例 #13
0
def main():
    """Main entry to the application
    """

    args = get_console_args()

    # Define the network's architecture
    architecture = JsonProcessor.load(args.architecture_file)

    # Initialize the Neural Network
    nn = NeuralNetwork(**architecture)

    # Set its parameters
    nn.set_parameters()

    # Loads the data
    nn.load_data(args.training_data)

    # Train the neural network
    nn.train(args.epochs)

    # Get its predictions
    predictions = nn.predict(JsonProcessor.load(args.testing_data)['input'])

    # Save its predictions
    JsonProcessor.beautify(args.output_file, {'output': predictions.tolist()})
コード例 #14
0
class RCDriverNNOnly(object):

    def __init__(self, host, port, serial_port, model_path):

        self.server_socket = socket.socket()
        self.server_socket.bind((host, port))
        self.server_socket.listen(0)

        # accept a single connection
        self.connection = self.server_socket.accept()[0].makefile('rb')

        # load trained neural network
        self.nn = NeuralNetwork()
        self.nn.load_model(model_path)

        self.rc_car = RCControl(serial_port)

    def drive(self):
        stream_bytes = b' '
        try:
            # stream video frames one by one
            while True:
                stream_bytes += self.connection.read(1024)
                first = stream_bytes.find(b'\xff\xd8')
                last = stream_bytes.find(b'\xff\xd9')

                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]
                    gray = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
                    image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)

                    # lower half of the image
                    height, width = gray.shape
                    roi = gray[int(height/2):height, :]

                    cv2.imshow('image', image)
                    # cv2.imshow('mlp_image', roi)

                    # reshape image
                    image_array = roi.reshape(1, int(height/2) * width).astype(np.float32)

                    # neural network makes prediction
                    prediction = self.nn.predict(image_array)
                    self.rc_car.steer(prediction)

                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        print("car stopped")
                        self.rc_car.stop()
                        break
        finally:
            cv2.destroyAllWindows()
            self.connection.close()
            self.server_socket.close()
コード例 #15
0
ファイル: server_video.py プロジェクト: bsy3719/posla
    def __init__(self, client, steer):        

        self.client = client        
        self.steer = steer

        self.stopline = StopLine.Stop()
        self.dect = Object.Object_Detection(self.steer)

        # model create
        self.model = NeuralNetwork()
        self.model.load_model(path = 'model_data/video_model_1.h5')          
コード例 #16
0
    def __init__(self, hidden_sizes, batch_size, learning_rate, activator):
        """The entry of project.

        Load data and build Neural Network model.
        """
        self.hidden_sizes = hidden_sizes
        self.batch_size = batch_size
        self.learning_rate = learning_rate
        self.activator = activator

        self.data = Data(SHELL_ARGS.prefix)
        self.model = NeuralNetwork(self.data.input_size(), self.hidden_sizes,
                                   self.data.output_size(), self.batch_size,
                                   self.activator)
コード例 #17
0
    def __init__(self, host, port, serial_port, model_path):

        self.server_socket = socket.socket()
        self.server_socket.bind((host, port))
        self.server_socket.listen(0)

        # accept a single connection
        self.connection = self.server_socket.accept()[0].makefile('rb')

        # load trained neural network
        self.nn = NeuralNetwork()
        self.nn.load_model(model_path)

        self.rc_car = RCControl(serial_port)
コード例 #18
0
class RCDriverNNOnly(object):
    def __init__(self, host_str, model_path):

        # load trained neural network
        self.nn = NeuralNetwork()
        self.nn.load_model(model_path)

        self.car_ctrl = sock_car_control()

    def drive(self):
        stream_bytes = b' '
        stream = urllib.request.urlopen(host_str)
        try:
            # stream video frames one by one
            while True:
                stream_bytes += stream.read(1024)
                first = stream_bytes.find(b'\xff\xd8')
                last = stream_bytes.find(b'\xff\xd9')

                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]
                    gray = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                        cv2.IMREAD_GRAYSCALE)
                    image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                         cv2.IMREAD_COLOR)

                    # lower half of the image
                    height, width = gray.shape
                    roi = gray[int(height / 2):height, :]

                    cv2.imshow('image', image)
                    # cv2.imshow('mlp_image', roi)

                    # reshape image
                    image_array = roi.reshape(1,
                                              int(height / 2) * width).astype(
                                                  np.float32)

                    # neural network makes prediction
                    prediction = self.nn.predict(image_array)
                    self.car_ctrl.steer(prediction)

                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        print("car stopped")
                        self.car_ctrl.stop()
                        break
        finally:
            cv2.destroyAllWindows()
            self.car_ctrl.close()
コード例 #19
0
    def __init__(self, client, steer):

        self.client = client
        self.steer = steer

        self.stopline = StopLine.Stop()
        self.objDetect = Object.Object_Detection(
            self.steer)  # class - load object detection model

        # model create
        print("Load VGGNet")
        self.model = NeuralNetwork()
        self.model.load_model(
            path='../model_data/posicar_binary_v11.h5')  # self-driving model
        print("Complete")
コード例 #20
0
    def __init__(self, model_path):
        print('initing...')


        # load trained neural network
        if model_num == 1:
            import model_b as model
            self.model = model
        elif model_num == 0:
            load_model_start = time.time()
            from model import NeuralNetwork
            self.model = NeuralNetwork(model_path)
            load_model_end = time.time()
            print('loading model costs {:02f} second(s)'.format(load_model_end - load_model_start))
        self.cap = cv2.VideoCapture(0)
        self.servo = PyServo.Servo(SerialID,Baudrate)
コード例 #21
0
def run():
    # Initialize size of network with
    inode = 3
    hnode = 4
    onode = 2

    n = NeuralNetwork(inode, hnode, onode)

    entry = [1.0, 0.5, -1.5]
    result = n.query(entry)

    print("Init:")
    print(n.w_input_hidden)

    target = list(result.T[0])
    n.train(entry, target)

    print("")
    print("Target:")
    print(n.w_input_hidden)

    target[0] += 1
    n.train(entry, target)

    print("")
    print("Error:")
    print(n.w_input_hidden)
コード例 #22
0
def run(data_obj, training_size):

    data_obj.split_dataset(training_size)
    data_obj.preprocess()

    #-------------------------------- Autoencoder model
    ae_model = AutoEncoder(data_obj.x_train_scaled.shape[1],
                           training_size, data_obj.name)
    ae_model.train(data_obj.x_train_scaled, data_obj.x_val_scaled)

    #-------------------------------- Encoded representation
    x_train_encoded, x_val_encoded, x_test_encoded = ae_model.encoded_data(
        data_obj.x_train_scaled, data_obj.x_val_scaled, data_obj.x_test_scaled)

    #-------------------------------- Neural Network model
    nn_model = NeuralNetwork(
        data_obj.x_train_scaled.shape[1], data_obj.y_train.shape[1],
        training_size, data_obj.name)
    nn_model.train(
        x_train_encoded, data_obj.y_train, x_val_encoded, data_obj.y_val)
    nn_model.evaluate(x_test_encoded, data_obj.y_test)

    #-------------------------------- reset data from memory
    data_obj.reset_scalar()

    return nn_model.result()
class VideoStreamHandler(socketserver.StreamRequestHandler):

    # load trained neural network
    model = NeuralNetwork()
    model.modified_model()
    model.load_model()

    # # hard coded thresholds for stopping, sensor 30cm, other two 25cm
    d_sensor_thresh = 15

    def handle(self):
        global prediction
        global sensor_data

        stream_bytes = b' '

        try:
            # stream video frames one by one
            while True:
                stream_bytes += self.rfile.read(1024)
                first = stream_bytes.find(b'\xff\xd8')
                last = stream_bytes.find(b'\xff\xd9')
                if first != -1 and last != -1:
                    jpg = stream_bytes[first:last + 2]
                    stream_bytes = stream_bytes[last + 2:]
                    gray = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                        cv2.IMREAD_GRAYSCALE)
                    image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8),
                                         cv2.IMREAD_COLOR)

                    # lower half of the image
                    height, width = gray.shape
                    roi = gray[int(height / 1.25):height, :]

                    cv2.imshow('image', image)

                    # reshape image
                    gray = (np.expand_dims(roi, 0))
                    gray = gray[:, :, :, np.newaxis]

                    # neural network makes prediction
                    prediction = self.model.predict(gray)

                    # stop conditions
                    if sensor_data and int(sensor_data) < self.d_sensor_thresh:
                        print("Dur, Nesne Algılandı...!\n")
                        prediction = 4
                        sensor_data = None

                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        print("car stopped")
                        break

        finally:
            cv2.destroyAllWindows()
            sys.exit()
コード例 #24
0
def joint_actuate(clientID, Body, primitive, frame):
    model = NeuralNetwork()
    model.load_state_dict(torch.load('C:\\Users\\lenovo\\Desktop\\AI-Project-Portfolio\\Amendments\\new_angle_mapping\\weights_2.pth'))
    model.eval()
    angle = model(torch.from_numpy(primitive[frame].reshape(1, -1)).float())
    HP, HY, LSP, LSR, LER, LEY, RSP, RSR, RER, REY, LHP, LHR, LHYP, LKP, RHP, RHR, RHYP, RKP, LAP, LAR, RAP, RAR = angle.detach().numpy().reshape(-1)
    print(LSP, LSR, LER, LEY)
    def control(joint, angel):
        sim.simxSetJointTargetPosition(clientID, Body[joint], angel, sim.simx_opmode_oneshot)
    sim.simxPauseCommunication(clientID,1)
    control('HeadPitch', HP)
    control('HeadYaw', HY)
    control('LShoulderPitch', 1*LSP)
    control('LShoulderRoll', 1*LSR)
    control('LElbowRoll', 1*LER)
    control('LElbowYaw', 1*LEY)
    control('RShoulderPitch', RSP)
    control('RShoulderRoll', RSR)
    control('RElbowRoll', RER)
    control('RElbowYaw', REY)
    control('LHipPitch', LHP)
    control('LHipRoll', LHR)
    control('LHipYawPitch', LHYP)
    control('LKneePitch', LKP)
    control('RHipPitch', RHP)
    control('RHipRoll', RHR)
    control('RHipYawPitch', RHYP)
    control('RKneePitch', RKP)
    control('LAnklePitch', LAP)
    control('LAnkleRoll', LAR)
    control('RAnklePitch', RAP)
    control('RAnkleRoll', RAR)
    sim.simxPauseCommunication(clientID,0)
    #time.sleep(.04)
コード例 #25
0
ファイル: qii.py プロジェクト: hovinh/IRIS_Transparency
    def use_QII_with_model(X_train):
        model = NeuralNetwork()
        model.create_graph()

        # get input data for each layer
        hl_1, hl_2, hl_3, prediction = model.predict(X_train)

        # PREDEFINE PARAMETERS
        # Number of features available for sampling, we want to observe
        # input layer, hence number of units is 4
        numb_features = 4

        # initializes QII class with name for each features, here we use index only
        feature_list = [str(i) for i in range(1, numb_features + 1)]
        influencer = QII(X_train, feature_list)

        # value of one data points we want to observe
        data_point = X_train[0]

        with tf.Session(graph=model.graph) as sess:
            saver = tf.train.Saver()
            saver.restore(sess, model.CHECK_POINT)

            # user can choose to calculate shapley by random sampling of permutation
            # or exhaustively check each of them with is_exhaustive

            # since 4! = 24 is not a big number, we can try both here
            print('Approximation with Random Sampling')
            shapley_val = influencer.shapley(data_point,
                                             model.predict_input,
                                             sess,
                                             is_exhaustive=False)
            print(shapley_val)

            print('Correct value with Exhausive Computation')
            shapley_val = influencer.shapley(data_point,
                                             model.predict_input,
                                             sess,
                                             is_exhaustive=True)
            print(shapley_val)
コード例 #26
0
ファイル: ES.py プロジェクト: karush17/esac
def crossover(args, STATE_DIM, ACTION_DIM, gene1, gene2):
    actor_1 = NeuralNetwork(STATE_DIM, ACTION_DIM.shape[0], args.hidden_size)
    actor_1.load_state_dict(gene1)
    actor_2 = NeuralNetwork(STATE_DIM, ACTION_DIM.shape[0], args.hidden_size)
    actor_2.load_state_dict(gene2)
    for param1, param2 in zip(actor_1.parameters(), actor_2.parameters()):

        # References to the variable tensors
        W1 = param1.data
        W2 = param2.data

        if len(W1.shape) == 2:  #Weights no bias
            num_variables = W1.shape[0]
            # Crossover opertation [Indexed by row]
            num_cross_overs = random.randrange(
                num_variables * 2)  # Lower bounded on full swaps
            for i in range(num_cross_overs):
                receiver_choice = random.random(
                )  # Choose which gene to receive the perturbation
                if receiver_choice < 0.5:
                    ind_cr = random.randrange(W1.shape[0])  #
                    W1[ind_cr, :] = W2[ind_cr, :]
                else:
                    ind_cr = random.randrange(W1.shape[0])  #
                    W2[ind_cr, :] = W1[ind_cr, :]

        elif len(W1.shape) == 1:  #Bias
            num_variables = W1.shape[0]
            # Crossover opertation [Indexed by row]
            num_cross_overs = random.randrange(
                num_variables)  # Lower bounded on full swaps
            for i in range(num_cross_overs):
                receiver_choice = random.random(
                )  # Choose which gene to receive the perturbation
                if receiver_choice < 0.5:
                    ind_cr = random.randrange(W1.shape[0])  #
                    W1[ind_cr] = W2[ind_cr]
                else:
                    ind_cr = random.randrange(W1.shape[0])  #
                    W2[ind_cr] = W1[ind_cr]
コード例 #27
0
ファイル: qii.py プロジェクト: hovinh/IRIS_Transparency
    def use_QII_with_model(X_train):
        model = NeuralNetwork()
        model.create_graph()
        hl_1, hl_2, hl_3, prediction = model.predict(X_train)
        numb_features = 4  # hidden layer 3 has 4 units
        feature_list = [str(i) for i in range(1, numb_features + 1)]
        influencer = QII(X_train, feature_list)

        # value of one data points we want to observe
        data_point = X_train[101]

        with tf.Session(graph=model.graph) as sess:
            saver = tf.train.Saver()
            saver.restore(sess, model.CHECK_POINT)
            observed_feature_list = [0, 1, 3]
            banzhaf_val = influencer.banzhaf(data_point,
                                             model.predict_input,
                                             sess,
                                             observed_feature_list,
                                             is_exhaustive=True)
            influence_scores = np.array([banzhaf_val[i] for i in feature_list])
            influence_scores = np.expand_dims(influence_scores, axis=0)
            influencer.plot_influence_score(influence_scores, feature_list)
コード例 #28
0
    def __init__(self, host, port, serial_port, model_path):

        self.server_socket = socket.socket()
        self.server_socket.bind((host, port))
        self.server_socket.listen(0)

        # accept a single connection
        self.connection = self.server_socket.accept()[0].makefile('rb')

        # load trained neural network
        self.nn = NeuralNetwork()
        self.nn.load_model(model_path)

        self.rc_car = RCControl(serial_port)
コード例 #29
0
def main():

    # ----DATA PREPARATION---- #
    x, y = make_moons(1000, noise=0.20)

    plt.scatter(x[:, 0], x[:, 1], s=10, c=y, cmap=plt.cm.Spectral)

    np.random.seed(0)
    np.random.shuffle(y)

    np.random.seed(0)
    np.random.shuffle(x)

    # ----MODEL TRAINING---- #
    settings = {
        'layer_sizes': [x.shape[1], 3, len(np.unique(y))],
        'epochs': 100,
        'alpha': 0.01,
        'lmbda': 0.001
    }

    nn = NeuralNetwork(**settings)

    nn.train(x, y)
コード例 #30
0
    def load(path):
        if not os.path.exists(path):
            print("Training can not be continued, cause it does not exist!")
            exit()

        with open(f"{path}/training_info.txt", "r") as info_file:
            lines = info_file.readlines()
            iterration    = int(lines[0])
            pop_size      = int(lines[1])
            sigma         = float(lines[2])
            learning_rate = float(lines[3])
            best_main     = float(lines[4])
            best_all      = float(lines[5])
        model = NeuralNetwork.load(f"{path}/model.h5")

        return Training(path, iterration, pop_size, sigma, learning_rate, model=model, best_main=best_main, best_all=best_all)
コード例 #31
0
    def __init__(self,
                 layer_sizes,
                 env_name,
                 invert_reward=True,
                 amount_threads=None,
                 cpu_only=True,
                 number_repeats=5,
                 seed=8,
                 reward_offset=0):
        self.reward_offset = reward_offset
        self.invert_reward = invert_reward
        self.amount_threads = multiprocessing.cpu_count(
        ) if amount_threads == None else amount_threads
        self.envs = [gym.make(env_name) for _ in range(self.amount_threads)]
        for env in self.envs:
            env.seed(seed)
        self.env_name = env_name
        input_size = self.envs[0].observation_space.shape[0]

        if isinstance(self.envs[0].action_space, spaces.Discrete):
            print(env_name, "has a discrete action space")
            output_activation = softmax
            output_size = self.envs[0].action_space.n
            self.discrete_action = True
            output_activation = softmax

        else:
            print(env_name, "has a non-discrete action space")
            output_activation = None
            output_size = self.envs[0].action_space.shape[0]
            self.discrete_action = False
            output_activation = np.tanh

        self.models = [
            NeuralNetwork(input_size=input_size,
                          hidden_sizes=layer_sizes,
                          output_size=output_size,
                          output_activation=output_activation,
                          discrete_action=self.discrete_action)
            for _ in range(self.amount_threads)
        ]

        self.solution_size = len(self.models[0].get_weights())
        self.number_repeats = number_repeats
        self.interrupt = False
コード例 #32
0
def train(args, data_info):
    n_user = data_info[0]
    n_item = data_info[1]
    train_data = data_info[2]
    val_data = data_info[3]
    test_data = data_info[4]

    model = NeuralNetwork(args, n_user, n_item)

    train_rmse_ls = []
    val_rmse_ls = []
    test_rmse_ls = []
    epoch = []

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for step in range(args.n_epochs):
            np.random.shuffle(train_data)
            start = random.randint(0, train_data.shape[0] - args.batch_size)
            _ = model.train(sess,
                            feed_dict=get_feed_dict(train_data, model, start,
                                                    start + args.batch_size))

            #start = 0
            #while start < len(train_data):
            #    model.train(sess, feed_dict=get_feed_dict(train_data, model, start, start + args.batch_size))
            #    start += args.batch_size

            train_loss, train_rmse = model.validation(sess,
                                                      feed_dict=get_feed_dict(
                                                          train_data, model, 0,
                                                          len(train_data)))
            val_loss, val_rmse = model.validation(sess,
                                                  feed_dict=get_feed_dict(
                                                      val_data, model, 0,
                                                      len(val_data)))
            test_loss, test_rmse = model.validation(sess,
                                                    feed_dict=get_feed_dict(
                                                        test_data, model, 0,
                                                        len(test_data)))

            train_rmse_ls.append(train_rmse)
            val_rmse_ls.append(val_rmse)
            test_rmse_ls.append(test_rmse)
            epoch.append(step)

            print(
                'epoch %d    train loss: %.4f  rmse: %.4f    val loss: %.4f  rmse: %.4f    test loss: %.4f  rmse: %.4f'
                % (step, train_loss, train_rmse, val_loss, val_rmse, test_loss,
                   test_rmse))

        return train_rmse_ls, val_rmse_ls, test_rmse_ls, epoch
コード例 #33
0
__author__ = 'zhengwang'

from model import load_data, NeuralNetwork

input_size = 120 * 320
data_path = "training_data/*.npz"

X_train, X_valid, y_train, y_valid = load_data(input_size, data_path)

# train a neural network
layer_sizes = [input_size, 32, 4]
nn = NeuralNetwork()
nn.create(layer_sizes)
nn.train(X_train, y_train)

# evaluate on train data
train_accuracy = nn.evaluate(X_train, y_train)
print("Train accuracy: ", "{0:.2f}%".format(train_accuracy * 100))

# evaluate on validation data
validation_accuracy = nn.evaluate(X_valid, y_valid)
print("Validation accuracy: ", "{0:.2f}%".format(validation_accuracy * 100))

# save model
model_path = "saved_model/nn_model.xml"
nn.save_model(model_path)