This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Print names using enhanced for loop | |
| List<String> names = List.of("Rahul", "Virat", "Sourav"); | |
| for(String name:names) { | |
| System.out.println(name); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Print a pyramid pattern using for loop | |
| for(int i = 1; i <= 5; i++) { | |
| for(int j = 1; j <= i; j++) { | |
| System.out.printf("%d ", j); | |
| } | |
| System.out.print("\n"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Program to print first 10 odd numbers using while loop | |
| int i = 1, count = 0; | |
| while(count != 10) { | |
| if(i%2==1) { | |
| count++; | |
| System.out.printf("%d ", i); | |
| } | |
| i++; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Program to perform summation on two numbers using do-while loop | |
| int a, b, c; | |
| Scanner scanner = new Scanner(System.in); | |
| do { | |
| System.out.println("Enter first number: "); | |
| a = scanner.nextInt(); | |
| System.out.println("Enter second number: "); | |
| b = scanner.nextInt(); | |
| System.out.printf("Sum is %d \n", a + b); | |
| System.out.println("Press 1 to add more numbers, 0 to exit: "); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Making life easier with fast.ai | |
| # required library -> fastai (https://github.com/fastai/fastai/) | |
| from fastai.collab import * | |
| from fastai.tabular import * | |
| # loading the dataframe | |
| ratings = pd.read_csv('ratings.csv') | |
| # creating data object | |
| data = CollabDataBunch.from_df(ratings, seed=42, valid_pct=0.1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def recommend_item_for_user(model, user_id): | |
| m = model.eval().cpu() | |
| user_ids = torch.LongTensor([user2idx[u] for u in [user_id]*len(items)]) | |
| item_ids = torch.LongTensor([item2idx[b] for b in items]) | |
| remove = set(ratings[ratings[user_col] == user_id][item_col].values) | |
| preds = m(user_ids,item_ids).detach().numpy() | |
| pred_item = [(p,b) for p,b in sorted(zip(preds,items), reverse = True) if b not in remove] | |
| return pred_item | |
| def recommend_user_for_item(model, item_id): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train(epochs=20, bs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def train(epochs = 10, bs = 64): | |
| for epoch in range(epochs): | |
| # training the model | |
| i=0 | |
| total_loss = 0.0 | |
| ct = 0 | |
| while i < len(data[0]): | |
| x1,x2,y = get_batch(data[0],i,i+bs) | |
| i+=bs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # create a model object | |
| # y_range has been extended(0-11) than required(1-10) to make the | |
| # values lie in the linear region of the sigmoid function | |
| model = EmbeddingModel(10, len(users), len(items), [0,11], initialise = 0.01).cuda() | |
| # split the data, returns a list [train, valid] | |
| data = get_data(ratings, 0.1) | |
| # loss = mean((target_rating - predicted_rating)**2) | |
| loss_function = nn.MSELoss() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # neural net based on Embedding matrices | |
| # model reference -> https://github.com/fastai/fastai/ | |
| class EmbeddingModel(nn.Module): | |
| def __init__(self, n_factors, n_users, n_items, y_range, initialise = 0.01): | |
| super().__init__() | |
| self.y_range = y_range | |
| self.u_weight = nn.Embedding(n_users, n_factors) | |
| self.i_weight = nn.Embedding(n_items, n_factors) | |
| self.u_bias = nn.Embedding(n_users, 1) | |
| self.i_bias = nn.Embedding(n_items, 1) |