Repository navigation
Tutorial
Clone (or download) the deep-learning-intro repository so you have a local copy. Inside the deep-learning-intro folder, create a file called tutorial.py and paste in the following code for importing the dataset:
import json
import os
import pprint
with open(os.path.join('data', 'videos.json')) as json_file:
videos = json.load(json_file)
print('We have {} videos'.format(len(videos)))
print("Data for the first video looks like this:")
pprint.pprint(videos[0])Now try to run the program (python tutorial.py). If you have everything set up correctly, it should print
We have 279 videos
{'relevant_topics': [],
'tags': ['Funny',
'smosh',
'video',
'Massively Multiplayer Online Role-playing Game (Video Game Genre)',
'World Of Warcraft (Video Game)',
'WoW',
'elf',
'warlords of draenor',
'smosh games',
'RPG',
'honest trailer',
'game trailer',
'Video Game (Industry)',
'Video Game Culture',
'Role-playing Game (Game Genre)',
'anthony',
'fun',
'ian',
'spoof',
'honest game trailers',
'orc',
'comedy',
'smoshgames',
'video games',
'honest',
'Warcraft (Fictional Universe)',
'games',
'MMORPG',
'parody'],
'target_category_id': 3,
'title': 'WORLD OF WARCRAFT (Honest Game Trailers)',
'topics': []}
As you can see, each video has a number of tags. For simplicity, let's use tags as our only input to the machine learning model we are going to make. First, we need to calculate a set that contains tags we are dealing with. Add the following snippet of python code to your tutorial.py:
tags = set()
for video in videos:
for tag in video["tags"]:
tags.add(tag)
num_tags = len(tags)
print("We have {} unique tags".format(num_tags))
This should print something like
We have 3184 unique tags
Since machine learning models work on vectors/matrices/tensors (all collections of numbers), we need to make a vector representation of each video. We can do that with binary vectors, where each position represents one tag, and the number at that position (either 0 or 1) represents the presence of that tag on the video.
tag_to_index = {tag: index for index, tag in enumerate(tags)}
def vectorize_video_input(video):
input_vector = [0] * num_tags
for tag in video["tags"]:
tag_index = tag_to_index.get(tag, None)
if tag_index is not None:
input_vector[tag_index] = 1
return input_vector
print('The first video in vector form looks like this:')
print(vectorize_video_input(videos[0]))
This will print something like this:
The first video in vector form looks like this:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, ... , 0, 0, 0, 0, 0, 0, 0]