예제 #1
0
    def initSetting(self):
        if not os.path.exists('setting.ini'):
            open('setting.ini', 'w')

        self.conf = configparser.ConfigParser()
        self.conf.read('setting.ini', encoding='utf-8')

        if not self.conf.has_section('paths'):
            self.conf.add_section('paths')
            self.conf.set('paths', 'dllpath',
                          r'C:\Segger\JLink_V692\JLink_x64.dll')
            self.conf.set(
                'paths', 'svdpath',
                r'["C:\Keil_v5\ARM\Packs\Keil\STM32F1xx_DFP\2.3.0\SVD\STM32F103xx.svd"]'
            )
            self.conf.set('paths', 'dispath',
                          r'["D:\Project\STM32_Blinky\out\STM32_Blinky.axf"]')
            self.conf.write(open('setting.ini', 'w', encoding='utf-8'))

        self.svdpaths = eval(self.conf.get('paths', 'svdpath'))
        self.dispaths = eval(self.conf.get('paths', 'dispath'))

        self.dllpath = self.conf.get('paths', 'dllpath')
        self.svdpath = self.svdpaths[0]
        self.dispath = self.dispaths[0]

        if os.path.isfile(self.svdpath):
            self.dev = svd.SVD(self.svdpath).device

            self.mcucore = self.dev.cpu.name

        else:
            self.mcucore = 'Cortex-M0'
예제 #2
0
    def do_path(self, subcmd=None, *path):
        '''display path, Syntax: path
set JLink_x64.dll, Syntax: path dll <dllpath>
set svd file path, Syntax: path svd <svdpath>
set dis file path, Syntax: path dis <dispath>\n'''
        if subcmd == None:
            maxlen = max(len(self.dllpath), len(self.svdpath),
                         len(self.dispath))
            print(
                f'{"√" if os.path.isfile(self.dllpath) else "×"}  {self.dllpath:{maxlen}}'
            )
            print(
                f'{"√" if os.path.isfile(self.svdpath) else "×"}  {self.svdpath:{maxlen}}'
            )
            print(
                f'{"√" if os.path.isfile(self.dispath) else "×"}  {self.dispath:{maxlen}}\n'
            )

        else:
            if path:
                path = ' '.join(path)
                match = re.match(r'%\w+%', path)
                if match:
                    path = path.replace(match.group(0),
                                        self.env[match.group(0)])

                if os.path.isfile(
                        path
                ):  # return True if path is an existing regular file
                    if subcmd == 'dll':
                        self.dllpath = path

                    elif subcmd == 'svd':
                        self.svdpath = path

                        self.dev = svd.SVD(self.svdpath).device
                        self.mcucore = self.dev.cpu.name

                    elif subcmd == 'dis':
                        self.dispath = path

                    else:
                        print(f'{subcmd} Unknown\n')

                    self.saveSetting()

                else:
                    print('Not exists or Not file\n')

            else:
                print('Input error\n')
예제 #3
0
 def sgd(self, data, matrix, mode):
     sgd = svd.SVD()
     if mode == 1:
         data.Mode1Matrix = [[0 for i in range(data.Num_K1)]
                             for j in range(data.Num_User)]
         data.Mode1Matrix = sgd.iteration(data, 500, matrix, data.Num_K1)
         return data.Mode1Matrix
     if mode == 2:
         data.Mode2Matrix = [[0 for i in range(data.Num_K2)]
                             for j in range(data.Num_Item)]
         data.Mode2Matrix = sgd.iteration(data, 500, matrix, data.Num_K2)
         return data.Mode2Matrix
     if mode == 3:
         data.Mode3Matrix = [[0 for i in range(data.Num_K3)]
                             for j in range(data.Num_Comtext)]
         data.Mode3Matrix = sgd.iteration(data, 500, matrix, data.Num_K3)
         return data.Mode3Matrix
if args.dataset not in ["100K", "1M", "10M", "20M"]:
    parser.error("Dataset does not exist")

dataset = args.dataset
n_splits = args.n_splits
n_features = args.n_features
n_epochs = args.n_epochs

# Fetch the training set, test set, list of users and movies and global bias from the dataset
train_sets, test_sets, user_list, movie_list, global_bias = fetch_dataset(
    dataset, n_splits)

rmses = []
# Train and measure the error for a new model for each (train_set, test_set) pair (or fold)
for (train_set, test_set) in zip(train_sets, test_sets):
    model = svd.SVD(train_set, n_features, user_list, movie_list, global_bias, \
        learning_rate=0.002, reg_factor=0.02, n_epochs=n_epochs, min_rating=1, max_rating=5)

    # Initialise the timer
    start_time = time.time()

    # Train the model
    model.train()

    # Print training time and fold number to the console
    time_postamble = "Train time: {}s".format(time.time() - start_time)
    print("-" * len(time_postamble))
    print("Fold:", len(rmses) + 1)
    print(time_postamble)

    # Obtain all the predicted ratings for the user, movie pairs in the test set
    predicted_ratings, true_ratings = [], []
n_features = int(sys.argv[2])
if n_features < 20:
    print("Usage: k must be greater than or equal to 20")
    sys.exit()
n_epochs   = int(sys.argv[3])

# Fetch data
train_sets, test_sets, user_list, movie_list, B = fetch_dataset(dataset, 5, shuffle=False)

for k in range(10, n_features+1, 10):
    # Only use one fold
    train_set = train_sets[0].copy()
    test_set  = test_sets[0].copy()

    # Create matrix factorisation model
    model = svd.SVD(train_set, k, user_list, movie_list, B, learning_rate=0.002, \
                reg_factor=0.02, n_epochs=1, min_rating=1, max_rating=5)

    # Keep track of rmses after every epoch
    rmses = []
    for epoch in range(n_epochs):
        model.train()

        predictions = []
        originals   = []

        # Calculate RMSE
        for i in range(test_set.shape[0]):
            user, movie, rating = int(test_set[i, 0]), int(test_set[i, 1]), test_set[i, 2]
            pred = model.compute_rating(user, movie, convert=True)
            predictions.append(pred)
            originals.append(rating)
예제 #6
0
import pre
import svd

data = pre.prepare(0.2)
para = {
    'gama': 0.007,
    'lambda4': 0.02,
    'lambda5': 0.005,
    'lambda6': 0.015,
    'K': 2,
    'epoch': 10
}

a = svd.SVD(para)
a.train(data)
result, rmse = a.test(data)

print rmse
예제 #7
0
def main():

    k = 4

    if len(sys.argv) < 2:
        print("ERROR : You need to specify the input file")
        quit()

    data_file = sys.argv[1]

    # Get sparse matrix of data
    data = process_data(data_loader.get_data(sys.argv[1]))
    a = svd.SVD(data)
    b = cur.CUR(data)

    start = time.clock()
    U, S, V = a.left_svd(k=k)
    stop = time.clock()

    reconst = np.dot(U, np.dot(S, V))
    print("U : ")
    print(U)
    print()
    print("S : ")
    print(S)
    print()
    print("V : ")
    print(V)
    print()
    print("Reconstructed SVD : ")
    print(reconst)
    print()
    print()

    print("TIme taken : ", stop - start)

    start = time.clock()

    prow, pcol = b.sample_probability()
    row_ind, rows = b.sample(4 * k, prow, "row")
    col_ind, cols = b.sample(4 * k, pcol, "col")

    print("C:")
    print(cols)
    print()

    #cols = cols.tocsr()
    #print(type(cols))

    W = cur.get_rows_from_col_matrix(cols, row_ind).T
    #print(W)

    print()

    #    print(W)
    U = cur.pinv(W)

    print("U:")
    print(U)

    print("R:")
    print(rows)
    print()

    print("CUR:")
    CUR = np.dot(cols.todense(), np.dot(U, rows.todense()))

    stop = time.clock()

    print(CUR)

    print("TIme taken : ", stop - start)
    sys.exit()

n_features = int(sys.argv[1])

names = ['userId', 'movieId', 'rating']
dtype = {'userId': np.uint32, 'movieId': np.uint32, 'rating': np.float64}
ratings = pd.read_csv('../data/movielens_1M/ratings.dat', sep="::", names=names, dtype=dtype, \
                      header=None, engine='python', usecols=[0,1,2])

# Form a list of unique users and movies
user_list = ratings['userId'].unique().tolist()
movie_list = ratings['movieId'].unique().tolist()

# Calculate the mean rating from all samples
B = ratings['rating'].mean()

for size in range(1, 51):
    curr_test_set = (ratings.sample(frac=(size / 50), random_state=7)).values

    # Create matrix factorisation model
    mf = svd.SVD(curr_test_set, n_features, user_list, movie_list, B, learning_rate=0.002, \
                reg_factor=0.02, n_epochs=5, min_rating=1, max_rating=5)

    # Time training and print result
    start_time = time.time()
    mf.train()
    time_postamble = "{:.2f} % of 1M dataset took {:.6f}s".format(
        (size / 50) * 100,
        time.time() - start_time)
    print(time_postamble)