diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Lesson.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Lesson.md new file mode 100644 index 0000000..0565fc9 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Lesson.md @@ -0,0 +1,49 @@ +# Working with Numbers + +## Working with Numbers +Welcome back to our **Redis course**! Now that you know how to connect to a Redis server, it's time to move forward and explore how to work with numbers in Redis. This unit builds on our previous lesson, so make sure you're comfortable establishing a connection to a Redis server. + +## What You'll Learn +In this lesson, you will learn how to: + +1. Set numeric values in Redis. +2. Retrieve and log numeric values. + +Here's the code snippet that we'll be working with: + +```JavaScript +import { createClient } from 'redis'; + +// Create and connect Redis client +const client = createClient({ url: 'redis://localhost:6379' }); + +client.on('error', (err) => console.log('Redis Client Error', err)); + +await client.connect(); + +// Setting and getting numeric values +await client.set('count', 5); +await client.set('completion_rate', 95.5); + +const count = await client.get('count'); +const completion_rate = await client.get('completion_rate'); + +console.log(`Course count: ${count}, Completion rate: ${completion_rate}`); + +await client.disconnect(); +``` + +Let's break down the code: + +* We use modern ES6 `import` syntax to import the `createClient` function from the `redis` library. +* The Redis client is created with `createClient` and connected to the Redis server at `'redis://localhost:6379'`. +* The client is set to log any connection errors. +* The connection to the Redis server is established using `await client.connect()`. +* We set numeric values using the `set` method: `count` with a value of `5` and `completion_rate` with a value of `95.5`. +* We retrieve these values using the `get` method. Note that in JavaScript, the return type of the `get` method is a string, so there's no need to decode it. +* Finally, the client disconnects from the Redis server using `await client.disconnect()`. + +## Why It Matters +Working with numbers in Redis is crucial because many real-world applications involve numeric data. From tracking user statistics to monitoring system performance, managing numbers in Redis allows you to perform a variety of useful operations efficiently. By mastering these basic operations with numbers, you'll be well-prepared to tackle more complex tasks and optimize your applications. + +Ready to dive in? Let's move on to the practice section and get hands-on experience working with numbers in Redis! diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice1/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice1/intro.md new file mode 100644 index 0000000..b45e5c2 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice1/intro.md @@ -0,0 +1,7 @@ +# Working with Numbers in Redis + +Great job on learning how to connect to a Redis server! Now, let's run the code you saw in the lesson to understand how to work with numbers in Redis. + +We will connect to a Redis server, set numeric values, retrieve them, and print them out. + +Run the following code to see how it all works together. \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice1/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice1/solution.js new file mode 100644 index 0000000..99e366b --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice1/solution.js @@ -0,0 +1,19 @@ +import { createClient } from 'redis'; + +// Create and connect Redis client +const client = createClient({ url: 'redis://localhost:6379' }); + +client.on('error', (err) => console.log('Redis Client Error', err)); + +await client.connect(); + +// Setting and getting numeric values +await client.set('count', 5); +await client.set('completion_rate', 95.5); + +const count = await client.get('count'); +const completion_rate = await client.get('completion_rate'); + +console.log(`Course count: ${count}, Completion rate: ${completion_rate}`); + +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice2/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice2/intro.md new file mode 100644 index 0000000..67f708b --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice2/intro.md @@ -0,0 +1,7 @@ +# Debug and Increment Redis Count + +Let's build on what you just learned. + +This task sets the `count` key in Redis to `5`. Then it retrieves the value of the `count` key and increments it by 1. + +However, the code is not working as expected. Can you debug the code and fix the issue? \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice2/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice2/solution.js new file mode 100644 index 0000000..aa1a9fb --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice2/solution.js @@ -0,0 +1,34 @@ +// import { createClient } from 'redis'; + +// // Create and connect Redis client +// const client = createClient({ url: 'redis://localhost:6379' }); + +// client.on('error', (err) => console.log('Redis Client Error', err)); + +// await client.connect(); + +// await client.set('count', 5); + +// let count = await client.get('count'); + +// console.log(`New course created. Total count: ${count + 1}`); + +// await client.disconnect(); + +import { createClient } from 'redis'; + +// Create and connect Redis client +const client = createClient({ url: 'redis://localhost:6379' }); + +client.on('error', (err) => console.log('Redis Client Error', err)); + +await client.connect(); + +await client.set('count', 5); + +let count = await client.get('count'); +count = parseInt(count); + +console.log(`New course created. Total count: ${count + 1}`); + +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice3/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice3/intro.md new file mode 100644 index 0000000..2386bf6 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice3/intro.md @@ -0,0 +1,5 @@ +# Filling Missing Parts to Work with Numbers in Redis + +Great progress so far! Let's move on to practicing adding missing pieces to our Redis script. + +In this task, you'll fill in the missing parts of the code to set and retrieve numeric values. Complete the code blocks where you see `TODO` comments. \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice3/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice3/solution.js new file mode 100644 index 0000000..2d302bf --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice3/solution.js @@ -0,0 +1,35 @@ +// import { createClient } from 'redis'; + +// // Create and connect Redis client +// const client = createClient({ url: 'redis://localhost:6379' }); + +// client.on('error', (err) => console.log('Redis Client Error', err)); + +// await client.connect(); + +// // TODO: Set the total number of students total_students and the average grade average_grade with values 10 and 88.5, respectively + +// // TODO: Retrieve the total number of students and the average grade values from Redis + +// // TODO: Log the values retrieved from Redis + +// await client.disconnect(); + +import { createClient } from 'redis'; + +// Create and connect Redis client +const client = createClient({ url: 'redis://localhost:6379' }); + +client.on('error', (err) => console.log('Redis Client Error', err)); + +await client.connect(); + +// TODO: Set the total number of students total_students and the average grade average_grade with values 10 and 88.5, respectively +client.set('total_students', 10); +client.set('average_grade', 88.5); +// TODO: Retrieve the total number of students and the average grade values from Redis +const total_students = await client.get('total_students'); +const average_grade = await client.get('average_grade'); +// TODO: Log the values retrieved from Redis +console.log(`Total students: ${total_students}, Average grade: ${average_grade}`); +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice4/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice4/intro.md new file mode 100644 index 0000000..94c8d13 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice4/intro.md @@ -0,0 +1,5 @@ +# Connecting to Redis and Working with Numeric Values + +Great progress so far! Now let's put your knowledge into action by writing the entire code yourself. + +Your task is to connect to a `Redis` server, set some numeric values, retrieve those values, and print them out. \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice4/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice4/solution.js new file mode 100644 index 0000000..e2b58f9 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part2/Practices/Practice4/solution.js @@ -0,0 +1,36 @@ +// TODO: Import the createClient function from the 'redis' module + +// TODO: Create a Redis client and connect to the Redis server at 'redis://localhost:6379' + +// TODO: Handle connection errors by logging them + +// TODO: Establish the connection to the Redis server + +// TODO: Set numeric values for 'count' and 'completion_rate' in the Redis server with values 5 and 95.5, respectively + +// TODO: Retrieve the values of 'count' and 'completion_rate' from the Redis server + +// TODO: Print the retrieved values for 'count' and 'completion_rate' + +// TODO: Disconnect the client from the Redis server + + + +// TODO: Import the createClient function from the 'redis' module +import { createClient } from 'redis'; +// TODO: Create a Redis client and connect to the Redis server at 'redis://localhost:6379' +const client = createClient({ url: 'redis://localhost:6379' }); +// TODO: Handle connection errors by logging them +client.on('error', (err) => console.log('Redis Client Error', err)); +// TODO: Establish the connection to the Redis server +await client.connect(); +// TODO: Set numeric values for 'count' and 'completion_rate' in the Redis server with values 5 and 95.5, respectively +client.set('count', 5); +client.set('completion_rate', 95.5); +// TODO: Retrieve the values of 'count' and 'completion_rate' from the Redis server +const count = await client.get('count'); +const completion_rate = await client.get('completion_rate'); +// TODO: Print the retrieved values for 'count' and 'completion_rate' +console.log(`Course count: ${count}, Completion rate: ${completion_rate}`); +// TODO: Disconnect the client from the Redis server +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Lesson.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Lesson.md new file mode 100644 index 0000000..f9344fa --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Lesson.md @@ -0,0 +1,55 @@ +# Operations with Numbers + +## Operations with Numbers +Welcome back! Now that you've learned how to work with numbers in **Redis**, it's time to build on that knowledge and explore some basic operations with these numbers. This lesson will show you how to perform operations like incrementing, decrementing, and modifying numeric values directly in Redis. + +## What You'll Learn +In this lesson, you will learn how to: + +1. Increment and decrement numeric values. +2. Modify numeric values using operations such as increments by a floating point. + +Here's the code snippet that we'll be working with: + +```JavaScript +import { createClient } from 'redis'; + +const client = createClient(); +client.on('error', (err) => console.log('Redis Client Error', err)); +await client.connect(); + +// Setting and getting string values +await client.set('count', 5); +await client.set('completion_rate', 95.5); +await client.set('duration', 0); + +await client.decr('count'); // 4 +await client.decrBy('count', 2); // 2 +await client.incr('duration'); // 1 +await client.incrBy('duration', 2); // 3 +await client.incrByFloat('completion_rate', 1.5); // 97 + +// Fetch the values and log them +const count = await client.get('count'); +const duration = await client.get('duration'); +const completion_rate = await client.get('completion_rate'); + +console.log(`Course count: ${count}`); // 2 +console.log(`Duration: ${duration}`); // 3 +console.log(`Completion rate: ${completion_rate}`); // 97 + +await client.disconnect(); +``` + +* After setting initial values for `count`, `completion_rate`, and `duration`, we perform various operations: + * `decr` operation decrements the value of `count` by 1, and `decrBy` decrements it by the specified value, in this case, 2. So, the final value of `count` is 2. + * `incr` operation increments the value of `duration` by 1, and `incrBy` increments it by the specified value, in this case, 2. So, the final value of `duration` is 3. + * `incrByFloat` increments the value of `completion_rate` by the specified floating-point value, in this case, 1.5. So, the final value of `completion_rate` is 97. +* At the end, we fetch the updated values of `count`, `duration`, and `completion_rate` and log them to the console. + +Note, that the `incr`, `decr`, `incrBy`, and `decrBy` operations cannot be applied to keys that contain floating-point values, that's why we use `incrByFloat` to increment floating-point values. Note, that in order to decrement floating-point values, you can use `incrByFloat` with a negative value. + +## Why It Matters +Understanding how to perform operations with numbers in Redis is essential for real-world applications. Imagine you're building a learning management system: you would track user progress, completion rates, and time spent on courses. Redis makes it fast and easy to update these numbers in real-time. + +By the end of this lesson, you'll be comfortable with basic numeric operations in Redis, preparing you for more advanced tasks. Ready to get started? Let's dive into the practice section and enhance your Redis skills! diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice1/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice1/intro.md new file mode 100644 index 0000000..bf1d026 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice1/intro.md @@ -0,0 +1,11 @@ +# Running Numeric Operations in Redis + +Now that you've learned how to work with numbers in Redis, let's run some code to see these operations in action. + +This code will: + +* Decrement the value of `count`. +* Increment the value of `completion_rate` by `1.5`. +* Increment the value of `duration`. + +Simply hit the Run button to see the code in action! \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice1/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice1/solution.js new file mode 100644 index 0000000..caf0443 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice1/solution.js @@ -0,0 +1,23 @@ +import { createClient } from 'redis'; + +const client = createClient(); +client.on('error', (err) => console.log('Redis Client Error', err)); +await client.connect(); + +await client.set('count', 5); +await client.set('completion_rate', 95.5); +await client.set('duration', 0); + +await client.decrBy('count', 1); +await client.incrByFloat('completion_rate', 1.5); +await client.incr('duration'); + +const count = await client.get('count'); +const completion_rate = await client.get('completion_rate'); +const duration = await client.get('duration'); + +console.log(`Course count: ${count}`); +console.log(`Completion rate: ${completion_rate}`); +console.log(`Duration: ${duration}`); + +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice2/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice2/intro.md new file mode 100644 index 0000000..174d3d6 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice2/intro.md @@ -0,0 +1,20 @@ +# Decrement and Increment Operations in Redis + +Great progress so far! Now, let's make our task a bit more interesting. + +Modify the existing Redis operations to: + +* Decrement the `completion_rate` by `2.5`. +* Increment the `duration` by `3`. + +This will help you see how changing operations can affect your data. + +Before you start, let's understand how the decrement by a float value works in Redis. In Redis, there is no direct command to decrement a key using a float value. However, you can use the `incrByFloat` command to decrement a key by a float value by providing a negative float value as an argument. + +Here's an example: + +```JavaScript +await client.incrByFloat('value', -1.1); // Decrement the value by 1.1 +``` + +Now let's get started with the task! \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice2/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice2/solution.js new file mode 100644 index 0000000..51c5406 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice2/solution.js @@ -0,0 +1,55 @@ +// import { createClient } from 'redis'; + +// const client = createClient(); +// client.on('error', (err) => console.log('Redis Client Error', err)); +// await client.connect(); + +// await client.set('count', 5); +// await client.set('completion_rate', 95.5); +// await client.set('duration', 0); + +// await client.decrBy('count', 1); + +// // TODO: Decrement the completion_rate by 2.5 +// await client.incrByFloat('completion_rate', 1.5); + +// // TODO: Increment the duration by 3. +// await client.incrBy('duration', 1); + +// const count = await client.get('count'); +// const completion_rate = await client.get('completion_rate'); +// const duration = await client.get('duration'); + +// console.log(`Course count: ${count}`); +// console.log(`Completion rate: ${completion_rate}`); +// console.log(`Duration: ${duration}`); + +// await client.disconnect(); + +import { createClient } from 'redis'; + +const client = createClient(); +client.on('error', (err) => console.log('Redis Client Error', err)); +await client.connect(); + +await client.set('count', 5); +await client.set('completion_rate', 95.5); +await client.set('duration', 0); + +await client.decrBy('count', 1); + +// TODO: Decrement the completion_rate by 2.5 +await client.incrByFloat('completion_rate', -2.5); + +// TODO: Increment the duration by 3. +await client.incrBy('duration', 3); + +const count = await client.get('count'); +const completion_rate = await client.get('completion_rate'); +const duration = await client.get('duration'); + +console.log(`Course count: ${count}`); +console.log(`Completion rate: ${completion_rate}`); +console.log(`Duration: ${duration}`); + +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice3/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice3/intro.md new file mode 100644 index 0000000..39c43b7 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice3/intro.md @@ -0,0 +1,3 @@ +# Fix the Redis Numeric Operations Bug + +Now, let's identify and fix a bug in the provided code. This snippet aims to perform various operations on numeric values in `Redis`. Your goal is to correct the mistake so that the script works as intended. \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice3/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice3/solution.js new file mode 100644 index 0000000..e403f99 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice3/solution.js @@ -0,0 +1,39 @@ +// import { createClient } from 'redis'; + +// const client = createClient(); +// client.on('error', (err) => console.log('Redis Client Error', err)); +// await client.connect(); + +// await client.set('count', 5); +// await client.set('duration', 0); + +// await client.decrBy('count', 1); +// await client.increment('duration'); + +// const count = await client.get('count'); +// const duration = await client.get('duration'); + +// console.log(`Course count: ${count}`); +// console.log(`Duration: ${duration}`); + +// await client.disconnect(); + +import { createClient } from 'redis'; + +const client = createClient(); +client.on('error', (err) => console.log('Redis Client Error', err)); +await client.connect(); + +await client.set('count', 5); +await client.set('duration', 0); + +await client.decrBy('count', 1); +await client.incr('duration'); + +const count = await client.get('count'); +const duration = await client.get('duration'); + +console.log(`Course count: ${count}`); +console.log(`Duration: ${duration}`); + +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice4/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice4/intro.md new file mode 100644 index 0000000..b4f4428 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice4/intro.md @@ -0,0 +1,5 @@ +# Incrementing and Decrementing Values in Redis + +Great job on your progress so far! Now, let's work on incrementing and decrementing values in `Redis`. + +We need to decrement the follower count, increment the percentage completion of a course by `4.1`, and increase the number of courses taken by `1`. The initial values are set for you. \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice4/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice4/solution.js new file mode 100644 index 0000000..9632183 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice4/solution.js @@ -0,0 +1,52 @@ +// import { createClient } from 'redis'; + +// const client = createClient(); +// client.on('error', (err) => console.log('Redis Client Error', err)); +// await client.connect(); + +// await client.set('followers', 2000); +// await client.set('percent_complete', 12.3); +// await client.set('courses_taken', 0); + +// // TODO: Decrement the number of followers by 1 + +// // TODO: Increment the percentage completion by 4.1 + +// // TODO: Increment the number of courses taken by 1 + +// const followers = await client.get('followers'); +// const percent_complete = await client.get('percent_complete'); +// const courses_taken = await client.get('courses_taken'); + +// console.log(`Followers: ${followers}`); +// console.log(`Percent Complete: ${percent_complete}`); +// console.log(`Courses Taken: ${courses_taken}`); + +// await client.disconnect(); + +import { createClient } from 'redis'; + +const client = createClient(); +client.on('error', (err) => console.log('Redis Client Error', err)); +await client.connect(); + +await client.set('followers', 2000); +await client.set('percent_complete', 12.3); +await client.set('courses_taken', 0); + +// TODO: Decrement the number of followers by 1 +await client.decr('followers'); +// TODO: Increment the percentage completion by 4.1 +await client.incrByFloat('percent_complete', 4.1); +// TODO: Increment the number of courses taken by 1 +await client.incr('courses_taken'); + +const followers = await client.get('followers'); +const percent_complete = await client.get('percent_complete'); +const courses_taken = await client.get('courses_taken'); + +console.log(`Followers: ${followers}`); +console.log(`Percent Complete: ${percent_complete}`); +console.log(`Courses Taken: ${courses_taken}`); + +await client.disconnect(); \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice5/intro.md b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice5/intro.md new file mode 100644 index 0000000..080ae06 --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice5/intro.md @@ -0,0 +1,7 @@ +# Performing Numeric Operations in Redis with JavaScript + +Your task is to perform numeric operations on Redis keys using JavaScript. + +This exercise will help you solidify your understanding of performing numeric operations in Redis. + +Follow the `TODO` comments in the starter code to complete the task. \ No newline at end of file diff --git a/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice5/solution.js b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice5/solution.js new file mode 100644 index 0000000..fd0871c --- /dev/null +++ b/Back-end Engineering/Learning and Mastering Redis with Node.js/Course1. Introduction to Redis with Node.js - The Basics/Part3/Practices/Practice5/solution.js @@ -0,0 +1,45 @@ +// import { createClient } from 'redis'; + +// const client = createClient(); +// client.on('error', (err) => console.log('Redis Client Error', err)); +// await client.connect(); + +// // TODO: Set initial values for 'stars', 'rating', and 'reviews' to 5, 4.5, and 10 respectively + +// // TODO: Decrease 'stars' by 1 + +// // TODO: Increase 'rating' by 0.5 + +// // TODO: Increase 'reviews' by 1 + +// // TODO: Retrieve and print the values of 'stars', 'rating', and 'reviews' + +// await client.disconnect(); + +import { createClient } from 'redis'; + +const client = createClient(); +client.on('error', (err) => console.log('Redis Client Error', err)); +await client.connect(); + +// TODO: Set initial values for 'stars', 'rating', and 'reviews' to 5, 4.5, and 10 respectively +await client.set('stars', 5); +await client.set('rating', 4.5); +await client.set('reviews', 10); +// TODO: Decrease 'stars' by 1 +await client.decr('stars'); +// TODO: Increase 'rating' by 0.5 +await client.incrByFloat('rating', 0.5); +// TODO: Increase 'reviews' by 1 +await client.incr('reviews'); +// TODO: Retrieve and print the values of 'stars', 'rating', and 'reviews' + +const stars = await client.get('stars'); +const rating = await client.get('rating'); +const reviews = await client.get('reviews'); + +console.log(`Stars: ${stars}`); +console.log(`Rating: ${rating}`); +console.log(`Reviews: ${reviews}`); + +await client.disconnect(); \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Lesson.md b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Lesson.md new file mode 100644 index 0000000..75f8add --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Lesson.md @@ -0,0 +1,124 @@ +# Processing Tensors with PyTorch Neural Network Layers + +## Introduction +Hello and welcome! Today, we will delve deeper into the world of tensor processing in PyTorch by discussing and implementing the crucial concepts of Linear Layers and Activation Functions. + +When working with tensors in neural networks, it is essential to understand that they are processed through various layers. A layer in a neural network refers to a collection of neurons (nodes) operating together at the same depth level within the network. PyTorch provides us with the `torch.nn` module, an easy and powerful tool for creating and organizing these layers. + +## Understanding the Linear Layer + +A vital part of most neural networks is the linear layer, which performs a linear transformation on its input data. A linear layer operates via the formula: + +\[ +y = Wx + b +\] + +Where \( y \) is the output, \( W \) represents the weight matrix, \( x \) is the input vector, and \( b \) indicates the bias vector. The weight matrix scales the input data, and the bias vector then shifts it, thereby producing the output. + +One of the powerful aspects of linear layers is their ability to transform the shape of the output as desired. By specifying the number of input and output features, you can control the dimensions of the tensor output from the layer. This flexibility allows the neural network to adapt to a variety of input shapes and deliver outputs that fit the requirements of the subsequent layers in the network. + +## Implementing a Linear Layer +In PyTorch, we commonly create these linear layers using the `nn.Linear()` function. Let's create such a layer, which will have 2 input features and 3 output features. + +```Python +import torch +import torch.nn as nn + +# Define an input tensor with specific values +input_tensor = torch.tensor([[1.0, 2.0]], dtype=torch.float32) + +# Create a linear layer with 2 input features and 3 output features +layer = nn.Linear(in_features=2, out_features=3) + +# Process the input through the linear layer to get initial output +output_tensor = layer(input_tensor) + +# Display the original input tensor +print(f"Input Tensor:\n{input_tensor}\n") + +# Display the output before activation to see the linear transformation effect +print(f"Output Tensor Before Activation:\n{output_tensor}\n") +``` + +The output of the above code will depend on the initial weights and biases, but an example might look like: + +``` +Input Tensor: +tensor([[1., 2.]]) + +Output Tensor Before Activation: +tensor([[ 0.2392, -1.2186, -0.9254]], grad_fn=) +``` + +The output tensor displayed above results from passing the input tensor through the linear layer. This layer applies a weighted sum of the input tensor values and adds a bias term to each output. The weights and biases are initialized randomly, so the exact output can vary. The `grad_fn=` in the output means that PyTorch is keeping track of this operation, which will help compute the gradients automatically during model training. + +## Understanding Activation Functions + +Activation functions introduce non-linearity into the model, enabling it to handle more complex patterns in the data. Two commonly used activation functions are ReLU (Rectified Linear Unit) and Sigmoid. + +Mathematically, ReLU is represented as: + +\[ +f(x) = \max(0, x) +\] + +Where \( x \) is the input to the function. The ReLU function ensures that positive input values remain unchanged, while negative ones are transformed to zero, creating a non-linear transformation. + +The Sigmoid function, on the other hand, is represented as: + +\[ +\sigma(x) = \frac{1}{1 + e^{-x}} +\] + +Where \( x \) is the input. The Sigmoid function squashes the input value to lie between 0 and 1, which can be useful for binary classification tasks. However, in practice, ReLU is often preferred over Sigmoid for hidden layers due to its simplicity and performance benefits. + +## Implementing the ReLU Activation Function +We can define a ReLU activation function in PyTorch using the `nn.ReLU()` function from the `torch.nn` module. Then, it can be applied to the output tensor from our linear layer. + +```Python +# Define a ReLU activation function to introduce non-linearity +relu = nn.ReLU() + +# Apply the ReLU function to the output of the linear layer +activated_output_relu = relu(output_tensor) + +# Display the output after activation to observe the effect of ReLU +print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}") +``` + +The output tensor after activation will look as follows: + +``` +Output Tensor After ReLU Activation: +tensor([[0.2392, 0.0000, 0.0000]], grad_fn=) +``` + +The output tensor after activation demonstrates the effect of the ReLU function. It zeroes out any negative values, converting them to zero, while keeping positive values unchanged. This introduces non-linearity into the model, which is crucial for handling more complex patterns in the data. The `grad_fn=` shows that the ReLU operation is also being tracked for automatic differentiation during training. + +## Implementing the Sigmoid Activation Function +Similarly, we can define and apply a Sigmoid activation function in PyTorch using the `nn.Sigmoid()` function from the `torch.nn` module. + +```Python +# Define a Sigmoid activation function +sigmoid = nn.Sigmoid() + +# Apply the Sigmoid function to the output of the linear layer +activated_output_sigmoid = sigmoid(output_tensor) + +# Display the output after applying the Sigmoid function +print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") +``` + +The output tensor after applying the Sigmoid activation will look as follows: + +``` +Output Tensor After Sigmoid Activation: +tensor([[0.5595, 0.2282, 0.2839]], grad_fn=) +``` + +The output tensor after activation shows the effect of the Sigmoid function. It squashes the input values to lie between 0 and 1. This can be particularly useful in scenarios where you want to interpret the output as probabilities. The `grad_fn=` indicates that the Sigmoid operation is tracked for automatic differentiation during training. + +## Lesson Summary +Great job! Today, we explored the concept of tensor processing through Linear Layers and Activation Functions in PyTorch. Through a practical code exercise, we learned how to use these two functions in combination to transform and process an input tensor. + +In the next set of exercises, you will have the opportunity to apply these concepts yourself actively. This practice will be crucial to solidifying your understanding of these concepts and your ability to process tensors effectively as you move forward in building more complex neural network architectures in PyTorch. Keep it up! diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices1/intro.md b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices1/intro.md new file mode 100644 index 0000000..21036a5 --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices1/intro.md @@ -0,0 +1,7 @@ +# Running Neural Network Layers in PyTorch + +You've succeeded so far in learning about PyTorch's Linear Layers and different Activation Functions! Let's put that knowledge into practice by running some provided Python code. + +The code creates and processes a `tensor` through a layer of the neural network. It uses the `ReLU` and `Sigmoid` activation functions to introduce non-linearity into the tensor transformation. Run this code carefully to consolidate your understanding and observe the outputs and the effects of both activation functions. + +Note: Before running, give us a moment to install PyTorch in the environment. We're setting up everything for your practice! 🛠️🔥 \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices1/solution.py b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices1/solution.py new file mode 100644 index 0000000..251ad5e --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices1/solution.py @@ -0,0 +1,35 @@ +import torch +import torch.nn as nn + +# Defining an input tensor +input_tensor = torch.tensor([[5.0, 6.0]], dtype=torch.float32) + +# Creating a linear layer with 2 input features and 3 output features +layer = nn.Linear(in_features=2, out_features=3) + +# Defining a ReLU activation function +relu = nn.ReLU() + +# Defining a Sigmoid activation function +sigmoid = nn.Sigmoid() + +# Processing the input through the linear layer +output_tensor = layer(input_tensor) + +# Applying the ReLU function to the output of the linear layer +activated_output_relu = relu(output_tensor) + +# Applying the Sigmoid function to the output of the linear layer +activated_output_sigmoid = sigmoid(output_tensor) + +# Displaying the original input tensor +print(f"Input Tensor:\n{input_tensor}\n") + +# Displaying the output before activation to see the linear transformation effect +print(f"Output Tensor Before Activation:\n{output_tensor}\n") + +# Displaying the output after activation to observe the effect of ReLU +print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") + +# Displaying the output after activation to observe the effect of Sigmoid +print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices2/intro.md b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices2/intro.md new file mode 100644 index 0000000..9959cd2 --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices2/intro.md @@ -0,0 +1,5 @@ +# Adjust Linear Layer Dimensions + +You've learned about Linear Layers and Activation Functions in PyTorch. + +Now, let's change the linear layer's input features to `3` and output features to `2`. This will help you see how modifying input and output dimensions affects the tensor's output. \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices2/solution.py b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices2/solution.py new file mode 100644 index 0000000..7b69463 --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices2/solution.py @@ -0,0 +1,71 @@ +# import torch +# import torch.nn as nn + +# # TODO: Define an input tensor with one more value +# input_tensor = torch.tensor([[1.0, 2.0]], dtype=torch.float32) + +# # TODO: Change the linear layer to have 3 input features and 2 output features +# layer = nn.Linear(in_features=2, out_features=3) + +# # Define a ReLU activation function +# relu = nn.ReLU() + +# # Define a Sigmoid activation function +# sigmoid = nn.Sigmoid() + +# # Process the input through the linear layer to get initial output +# output_tensor = layer(input_tensor) + +# # Apply the ReLU function to the output of the linear layer +# activated_output_relu = relu(output_tensor) + +# # Apply the Sigmoid function to the output of the linear layer +# activated_output_sigmoid = sigmoid(output_tensor) + +# # Display the original input tensor +# print(f"Input Tensor:\n{input_tensor}\n") + +# # Display the output before activation to see the linear transformation effect +# print(f"Output Tensor Before Activation:\n{output_tensor}\n") + +# # Display the output after activation to observe the effect of ReLU +# print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") + +# # Display the output after activation to observe the effect of Sigmoid +# print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") + +import torch +import torch.nn as nn + +# TODO: Define an input tensor with one more value +input_tensor = torch.tensor([[1.0, 2.0, 3.0]], dtype=torch.float32) + +# TODO: Change the linear layer to have 3 input features and 2 output features +layer = nn.Linear(in_features=3, out_features=2) + +# Define a ReLU activation function +relu = nn.ReLU() + +# Define a Sigmoid activation function +sigmoid = nn.Sigmoid() + +# Process the input through the linear layer to get initial output +output_tensor = layer(input_tensor) + +# Apply the ReLU function to the output of the linear layer +activated_output_relu = relu(output_tensor) + +# Apply the Sigmoid function to the output of the linear layer +activated_output_sigmoid = sigmoid(output_tensor) + +# Display the original input tensor +print(f"Input Tensor:\n{input_tensor}\n") + +# Display the output before activation to see the linear transformation effect +print(f"Output Tensor Before Activation:\n{output_tensor}\n") + +# Display the output after activation to observe the effect of ReLU +print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") + +# Display the output after activation to observe the effect of Sigmoid +print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices3/intro.md b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices3/intro.md new file mode 100644 index 0000000..ccc79aa --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices3/intro.md @@ -0,0 +1,7 @@ +# Fix Linear Layer Usage + +Great progress in understanding linear layers and activation functions. Now, let's take it a step further by debugging a piece of code. + +The provided code should create a `linear layer` and apply both `ReLU` and `Sigmoid` activation functions. However, there's an issue preventing it from working correctly. + +Your task is to identify and correct the error to ensure the code runs smoothly. This will improve your debugging skills and deepen your understanding of `PyTorch` functionality. \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices3/solution.py b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices3/solution.py new file mode 100644 index 0000000..c69bfa0 --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices3/solution.py @@ -0,0 +1,71 @@ +# import torch +# import torch.nn as nn + +# # Define an input tensor with specific values +# input_tensor = torch.tensor([[1.0, 2.0]], dtype=torch.float32) + +# # Create a linear layer with 2 input features and 3 output features +# layer = nn.Linear(in_features=3, out_features=3) + +# # Define a ReLU activation function +# relu = nn.ReLU() + +# # Define a Sigmoid activation function +# sigmoid = nn.Sigmoid() + +# # Process the input through the linear layer to get initial output +# output_tensor = layer(input_tensor) + +# # Apply the ReLU function to the output of the linear layer +# activated_output_relu = relu(output_tensor) + +# # Apply the Sigmoid function to the ReLU-activated output +# activated_output_sigmoid = sigmoid(activated_output_relu) + +# # Display the original input tensor +# print(f"Input Tensor:\n{input_tensor}\n") + +# # Display the output before activation to see the linear transformation effect +# print(f"Output Tensor Before Activation:\n{output_tensor}\n") + +# # Display the output after activation to observe the effect of ReLU +# print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") + +# # Display the output after activation to observe the effect of Sigmoid +# print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") + +import torch +import torch.nn as nn + +# Define an input tensor with specific values +input_tensor = torch.tensor([[1.0, 2.0]], dtype=torch.float32) + +# Create a linear layer with 2 input features and 3 output features +layer = nn.Linear(in_features=2, out_features=3) + +# Define a ReLU activation function +relu = nn.ReLU() + +# Define a Sigmoid activation function +sigmoid = nn.Sigmoid() + +# Process the input through the linear layer to get initial output +output_tensor = layer(input_tensor) + +# Apply the ReLU function to the output of the linear layer +activated_output_relu = relu(output_tensor) + +# Apply the Sigmoid function to the ReLU-activated output +activated_output_sigmoid = sigmoid(activated_output_relu) + +# Display the original input tensor +print(f"Input Tensor:\n{input_tensor}\n") + +# Display the output before activation to see the linear transformation effect +print(f"Output Tensor Before Activation:\n{output_tensor}\n") + +# Display the output after activation to observe the effect of ReLU +print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") + +# Display the output after activation to observe the effect of Sigmoid +print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices4/intro.md b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices4/intro.md new file mode 100644 index 0000000..6052c6a --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices4/intro.md @@ -0,0 +1,5 @@ +# Defining and Applying Activation Functions in PyTorch + +Fantastic progress so far! In this exercise, part of the code has been given to you, where the lines involving defining and applying the `ReLU` activation function are missing. Your task is to fill in the lines marked with `TODO` to complete the script. + +Let's solidify your knowledge! \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices4/solution.py b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices4/solution.py new file mode 100644 index 0000000..2248bf6 --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices4/solution.py @@ -0,0 +1,51 @@ +# import torch +# import torch.nn as nn + +# input_tensor = torch.tensor([[1.0, 2.0]], dtype=torch.float32) + +# # Create linear layer with specified number of input and output features +# layer = nn.Linear(in_features=2, out_features=3) + +# # Process the input tensor through the linear layer to get initial output +# output_tensor = layer(input_tensor) + +# # TODO: Define a ReLU activation function + +# # TODO: Apply the ReLU function to the output tensor + +# # Define a Sigmoid activation function +# sigmoid = nn.Sigmoid() + +# # Apply the Sigmoid function to the output tensor +# activated_output_sigmoid = sigmoid(output_tensor) + +# print(f"Input Tensor:\n{input_tensor}\n") +# print(f"Output Tensor Before Activation:\n{output_tensor}\n") +# print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") +# print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") + +import torch +import torch.nn as nn + +input_tensor = torch.tensor([[1.0, 2.0]], dtype=torch.float32) + +# Create linear layer with specified number of input and output features +layer = nn.Linear(in_features=2, out_features=3) + +# Process the input tensor through the linear layer to get initial output +output_tensor = layer(input_tensor) + +# TODO: Define a ReLU activation function +relu = nn.ReLU() +# TODO: Apply the ReLU function to the output tensor +activated_output_relu = relu(output_tensor) +# Define a Sigmoid activation function +sigmoid = nn.Sigmoid() + +# Apply the Sigmoid function to the output tensor +activated_output_sigmoid = sigmoid(output_tensor) + +print(f"Input Tensor:\n{input_tensor}\n") +print(f"Output Tensor Before Activation:\n{output_tensor}\n") +print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") +print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices5/intro.md b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices5/intro.md new file mode 100644 index 0000000..09b28c6 --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices5/intro.md @@ -0,0 +1,7 @@ +# Processing Tensors with Neural Networks + +You have made excellent progress in understanding linear layers and activation functions in PyTorch. + +In this task, you'll apply what you've learned by writing the complete code to create a `linear` layer, and implement `ReLU` and `Sigmoid` activation functions on the linear output. + +This exercise will solidify your ability to create and manipulate neural network layers in PyTorch. \ No newline at end of file diff --git a/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices5/solution.py b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices5/solution.py new file mode 100644 index 0000000..b9399dd --- /dev/null +++ b/Machine Learning/Comprehensive Introduction to PyTorch/Course1. Introduction to PyTorch Tensors/Part5/Practices/Practices5/solution.py @@ -0,0 +1,52 @@ +# import torch +# import torch.nn as nn + +# # Define an input tensor with specific values +# input_tensor = torch.tensor([[7.5, 3.0, 9.9]], dtype=torch.float32) + +# # TODO: Create a linear layer with 3 input features and 4 output features + +# # TODO: Define a ReLU activation function + +# # TODO: Define a Sigmoid activation function + +# # TODO: Process the input through the linear layer to get initial output + +# # TODO: Apply the ReLU function to the output of the linear layer + +# # TODO: Apply the Sigmoid function to the output of the linear layer + +# # TODO: Display the original input tensor + +# # TODO: Display the output before activation to see the linear transformation effect + +# # TODO: Display the output after activation to observe the effect of ReLU + +# # TODO: Display the output after activation to observe the effect of Sigmoid + +import torch +import torch.nn as nn + +# Define an input tensor with specific values +input_tensor = torch.tensor([[7.5, 3.0, 9.9]], dtype=torch.float32) + +# TODO: Create a linear layer with 3 input features and 4 output features +layer = nn.Linear(in_features=3, out_features=4) +# TODO: Define a ReLU activation function +relu = nn.ReLU() +# TODO: Define a Sigmoid activation function +sigmoid = nn.Sigmoid() +# TODO: Process the input through the linear layer to get initial output +output_tensor = layer(input_tensor) +# TODO: Apply the ReLU function to the output of the linear layer +activated_output_relu = relu(output_tensor) +# TODO: Apply the Sigmoid function to the output of the linear layer +activated_output_sigmoid = sigmoid(output_tensor) +# TODO: Display the original input tensor +print(f"Input Tensor:\n{input_tensor}\n") +# TODO: Display the output before activation to see the linear transformation effect +print(f"Output Tensor Before Activation:\n{output_tensor}\n") +# TODO: Display the output after activation to observe the effect of ReLU +print(f"Output Tensor After ReLU Activation:\n{activated_output_relu}\n") +# TODO: Display the output after activation to observe the effect of Sigmoid +print(f"Output Tensor After Sigmoid Activation:\n{activated_output_sigmoid}") \ No newline at end of file