示例#1
0
    def predict(self, x_feat):
        """Predict regression scores.

        Argument:
            x_feat: feature matrix of shape (?, n_feature).

        Return:
            score matrix of shape (?, n_label).
        """
        return self.model.predict(utils.add_index(x_feat), batch_size=self.bs)
示例#2
0
 def receive(self):
     #self.logger.info("Receiving on port: {} and replying with ACK on port: {}".format(self.inbound_port, self.outbound_port))
     while True:
         try:
             if self.index < 0 or self.last:
                self.logger.info("Stopping now") 
             #    sys.stdout.write(self.fullData)
                sys.exit()
                break
             data = self.simulator.u_receive()  # receive data
             real_data,isError,hsh,index = utils.check_checksum(data)
             self.logger.info("reciving frame {} current index is {}".format(index,self.index))
             if self.first and (not isError):
                 self.logger.info("first has run")
                 self.index = index
                 self.first = False 
             if isError or (index != self.index):
                 if index != self.index and not isError:
                     # self.logger.info("ACK being sent {}".format(utils.add_index(MyReceiver.ACK_DATA, index,self.logger)))
                     myAck = utils.add_index(MyReceiver.ACK_DATA, index)
                     self.logger.info("ack value being sent from error {}".format(myAck))
                     self.simulator.u_send(myAck)
                 else:
                     # self.logger.info("ACK being sent {}".format(utils.add_index(MyReceiver.NACK_DATA, index,self.logger)))
                     myNACK = utils.add_index(MyReceiver.NACK_DATA,index)
                     self.logger.info("nack value being sent {}".format(myNACK))
                     self.simulator.u_send(myNACK)
                 # else send nack
                 continue
             if self.index == 0:
                 self.logger.info("setting last to true")
                 self.last = True
             self.index -= 1 
             sys.stdout.write(real_data)
             # self.fullData += real_data
             # self.logger.info("ACK being sent {}".format(utils.add_index(MyReceiver.ACK_DATA, index,self.logger)))
             myAck = utils.add_index(MyReceiver.ACK_DATA, index)
             self.logger.info("ACK value being sent {}".format(myAck))
             self.simulator.u_send(myAck)  # send ACK
         except socket.timeout:
             pass
    def predict(self, x_feat):
        """Predict regression scores.

        Argument:
            x_feat: feature matrix of shape (?, n_feature).

        Return:
            score matrix of shape (?, n_label).
        """
        #self.model=model = load_model('my_model')
        self.model.load_weights('my_model_weights.h5')
        print(self.model)
        return self.model.predict(utils.add_index(x_feat), batch_size=self.bs)
示例#4
0
# -*- coding:utf-8 -*-
示例#5
0
    def fit(self,
            x_train,
            y_train,
            x_val,
            y_val,
            epochs,
            seed=1,
            n_train_sample=10000):
        """Train the model.

        Arguments:
            x_train: feature matrix of shape (n_train, n_feature).
            y_train: label matrix of shape (n_train, n_label).
            x_val:   feature matrix for validation.
            y_val:   label matrix for validation.
            epochs:  list of epochs when the error is calculated.

        Return:
            res:     dictionary with key: epoch, value: (train_error, test_error).
        """
        assert self.n_label == y_train.shape[1]
        np.random.seed(seed)

        x_train = utils.add_index(x_train)
        x_val = utils.add_index(x_val)

        bs = self.bs
        res = dict()

        initial_epoch = 0
        train_sec = 0  # training time in seconds
        n, _ = x_train.shape

        # Subsample training data for fast estimation of training loss.
        inx = np.random.choice(n, min(n, n_train_sample), replace=False)
        x_sample, y_sample = x_train[inx], y_train[inx]

        for epoch in epochs:
            start = time.time()
            for _ in range(epoch - initial_epoch):
                epoch_ids = np.random.choice(n, n / bs * bs, replace=False)
                for batch_ids in np.array_split(epoch_ids, n / bs):
                    x_batch, y_batch = x_train[batch_ids], y_train[batch_ids]
                    self.model.train_on_batch(x_batch, y_batch)

            train_sec += time.time() - start
            tr_score = self.model.evaluate(x_sample,
                                           y_sample,
                                           batch_size=bs,
                                           verbose=0)
            tv_score = self.model.evaluate(x_val,
                                           y_val,
                                           batch_size=bs,
                                           verbose=0)

            print(
                "train error: %.2f%%\tval error: %.2f%% (%d epochs, %.2f seconds)\t"
                "train l2: %.2e\tval l2: %.2e" %
                ((1 - tr_score[1]) * 100, (1 - tv_score[1]) * 100, epoch,
                 train_sec, tr_score[0], tv_score[0]))
            res[epoch] = (tr_score, tv_score)
            initial_epoch = epoch

        return res
input_shape = (D + 1, )  # n_feature, (sample) index
ix = Input(shape=input_shape, dtype='float32', name='indexed-feat')
x, index = utils.separate_index(ix)  # features, sample_id
kfeat = KernelEmbedding(kernel, x_train, input_shape=(D, ))(x)
y = Dense(num_classes,
          input_shape=(n, ),
          activation='linear',
          kernel_initializer='zeros',
          use_bias=False)(kfeat)

model = Model(ix, y)
model.compile(loss='mse',
              optimizer=PSGD(pred_t=y, index_t=index, eta=eta),
              metrics=['accuracy'])
trainers['Pegasos'] = Trainer(model=model,
                              x_train=utils.add_index(x_train),
                              x_test=utils.add_index(x_test))

# Assemble kernel EigenPro trainer.
embed = Model(ix, kfeat)
y = Dense(num_classes,
          input_shape=(n, ),
          activation='linear',
          kernel_initializer='zeros',
          use_bias=False)(kfeat)
model = Model(ix, y)
model.compile(loss='mse',
              optimizer=PSGD(pred_t=y,
                             index_t=index,
                             eta=scale * eta,
                             eigenpro_f=lambda g: kf(g, kfeat)),
示例#7
0
# -*- coding:utf-8 -*-
示例#8
0
# -*- coding:utf-8 -*-