Python
 Computer >> コンピューター >  >> プログラミング >> Python

Tensorflowを使用してPythonでIMDBデータセットをダウンロードおよび探索するにはどうすればよいですか?


Tensorflowは、Googleが提供する機械学習フレームワークです。これは、Pythonと組み合わせて使用​​されるオープンソースのフレームワークであり、アルゴリズム、深層学習アプリケーションなどを実装します。研究や生産目的で使用されます。

これは、NumPyと多次元配列を使用しているためです。これらの多次元配列は「テンソル」とも呼ばれます。フレームワークは、ディープニューラルネットワークの操作をサポートします。これは非常にスケーラブルであり、多くの一般的なデータセットが付属しています。 GPU計算を使用し、リソースの管理を自動化します。多数の機械学習ライブラリが付属しており、十分にサポートされ、文書化されています。フレームワークには、ディープニューラルネットワークモデルを実行し、それらをトレーニングし、それぞれのデータセットの関連する特性を予測するアプリケーションを作成する機能があります。

「tensorflow」パッケージは、以下のコード行を使用してWindowsにインストールできます-

pip install tensorflow

Tensorは、TensorFlowで使用されるデータ構造です。フロー図のエッジを接続するのに役立ちます。このフロー図は「データフローグラフ」と呼ばれます。テンソルは、多次元配列またはリストに他なりません。これらは、3つの主要な属性を使用して識別できます-

「IMDB」データセットには、5万本を超える映画のレビューが含まれています。このデータセットは通常、自然言語処理に関連する操作で使用されます。

Google Colaboratoryを使用して、以下のコードを実行しています。 Google ColabまたはColaboratoryは、ブラウザー上でPythonコードを実行するのに役立ち、構成が不要で、GPU(グラフィックプロセッシングユニット)に無料でアクセスできます。 ColaboratoryはJupyterNotebookの上に構築されています。

以下はコードです-

import matplotlib.pyplot as plt
import os
import re
import shutil
import string
import tensorflow as tf

from tensorflow.keras import layers
from tensorflow.keras import losses
from tensorflow.keras import preprocessing
from tensorflow.keras.layers.experimental.preprocessing import TextVectorization
print("The tensorflow version is ")
print(tf.__version__)
url = "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz"

dataset = tf.keras.utils.get_file("aclImdb_v1.tar.gz", url,
                                    untar=True, cache_dir='.',
                                    cache_subdir='')
print("The dataset is being downloaded")
dataset_dir = os.path.join(os.path.dirname(dataset), 'aclImdb')
print("The directories in the downloaded folder are ")
os.listdir(dataset_dir)

train_dir = os.path.join(dataset_dir, 'train')
os.listdir(train_dir)
print("The sample of data : ")
sample_file = os.path.join(train_dir, 'pos/1181_9.txt')
with open(sample_file) as f:
  print(f.read())

remove_dir = os.path.join(train_dir, 'unsup')
shutil.rmtree(remove_dir)
batch_size = 32
seed = 42
print("The batch size is")
print(batch_size)

raw_train_ds = tf.keras.preprocessing.text_dataset_from_directory(
    'aclImdb/train',
    batch_size=batch_size,
    validation_split=0.2,
    subset='training',
    seed=seed)

for text_batch, label_batch in raw_train_ds.take(1):
  for i in range(3):
    print("Review", text_batch.numpy()[i])
    print("Label", label_batch.numpy()[i])
 
print("Label 0 corresponds to", raw_train_ds.class_names[0])
print("Label 1 corresponds to", raw_train_ds.class_names[1])
raw_val_ds = tf.keras.preprocessing.text_dataset_from_directory(
    'aclImdb/train',
    batch_size=batch_size,
    validation_split=0.2,
    subset='validation',
    seed=seed)
raw_test_ds = tf.keras.preprocessing.text_dataset_from_directory(
    'aclImdb/test',
    batch_size=batch_size)

コードクレジット − https://www.tensorflow.org/tutorials/keras/text_classification

出力

The tensorflow version is
2.4.0
The dataset is being downloaded
The directories in the downloaded folder are
The sample of data :
Rachel Griffiths writes and directs this award winning short film. A heartwarming story about coping with grief and cherishing the memory of those we've loved and lost. Although, only 15 minutes long, Griffiths manages to capture so much emotion and truth onto film in the short space of time. Bud Tingwell gives a touching performance as Will, a widower struggling to cope with his wife's death. Will is confronted by the harsh reality of loneliness and helplessness as he proceeds to take care of Ruth's pet cow, Tulip. The film displays the grief and responsibility one feels for those they have loved and lost. Good cinematography, great direction, and superbly acted. It will bring tears to all those who have lost a loved one, and survived.
The batch size is
32
Found 25000 files belonging to 2 classes.
Using 20000 files for training.
Review b'"Pandemonium" is a horror movie spoof that comes off more stupid than funny. Believe me when I tell you, I love comedies. Especially comedy spoofs. "Airplane", "The Naked Gun" trilogy, "Blazing Saddles", "High Anxiety", and "Spaceballs" are some of my favorite comedies that spoof a particular genre. "Pandemonium" is not up there with those films. Most of the scenes in this movie had me sitting there in stunned silence because the movie wasn\'t all that funny. There are a few laughs in the film, but when you watch a comedy, you expect to laugh a lot more than a few times and that\'s all this film has going for it. Geez, "Scream" had more laughs than this film and that was more of a horror film. How bizarre is that?

*1/2 (out of four)'
Label 0
Review b"David Mamet is a very interesting and a very un-equal director. His first movie 'House of Games' was the one I liked best, and it set a series of films with characters whose perspective of life changes as they get into complicated situations, and so does the perspective of the viewer.

So is 'Homicide' which from the title tries to set the mind of the viewer to the usual crime drama. The principal characters are two cops, one Jewish and one Irish who deal with a racially charged area. The murder of an old Jewish shop owner who proves to be an ancient veteran of the Israeli Independence war triggers the Jewish identity in the mind and heart of the Jewish detective.

This is were the flaws of the film are the more obvious. The process of awakening is theatrical and hard to believe, the group of Jewish militants is operatic, and the way the detective eventually walks to the final violent confrontation is pathetic. The end of the film itself is Mamet-like smart, but disappoints from a human emotional perspective.

Joe Mantegna and William Macy give strong performances, but the flaws of the story are too evident to be easily compensated."
Label 0
Review b'Great documentary about the lives of NY firefighters during the worst terrorist attack of all time.. That reason alone is why this should be a must see collectors item.. What shocked me was not only the attacks, but the"High Fat Diet" and physical appearance of some of these firefighters. I think a lot of Doctors would agree with me that,in the physical shape they were in, some of these firefighters would NOT of made it to the 79th floor carrying over 60 lbs of gear. Having said that i now have a greater respect for firefighters and i realize becoming a firefighter is a life altering job. The French have a history of making great documentary\'s and that is what this is, a Great Documentary.....'
Label 1
Label 0 corresponds to neg
Label 1 corresponds to pos
Found 25000 files belonging to 2 classes.
Using 5000 files for validation.
Found 25000 files belonging to 2 classes.

説明

  • 必要なパッケージがインポートされ、エイリアス化されます。

  • ImdBデータは、Colabがアクセスできる場所にロードおよび保存されます。

  • 元のデータのサンプルがコンソールに表示されます。

  • 元のデータはトレーニングデータセットとテストデータセットに分割されます。

  • トレーニングデータはモデルの構築に使用されます。

  • 与えられたデータは、否定的なレビューまたは肯定的な値に分類されようとします。


  1. TensorFlowを使用して、PythonのIMDBデータセットの時間に関する精度と損失を視覚化するプロットを作成するにはどうすればよいですか?

    Tensorflowは、Googleが提供する機械学習フレームワークです。これは、Pythonと組み合わせて使用​​されるオープンソースのフレームワークであり、アルゴリズム、深層学習アプリケーションなどを実装します。研究や生産目的で使用されます。 「IMDB」データセットには、5万本を超える映画のレビューが含まれています。このデータセットは通常、自然言語処理に関連する操作で使用されます。 Google Colaboratoryを使用して、以下のコードを実行しています。 Google ColabまたはColaboratoryは、ブラウザー上でPythonコードを実行するのに役立ち、構成が不要

  2. TensorFlowを使用して、Pythonを使用してテンソルを作成し、メッセージを表示するにはどうすればよいですか?

    Tensorflowは、Googleが提供する機械学習フレームワークです。これは、Pythonと組み合わせて使用​​されるオープンソースのフレームワークであり、アルゴリズム、深層学習アプリケーションなどを実装します。それは研究および生産目的で使用されます。複雑な数学演算をすばやく実行するのに役立つ最適化手法があります。 これは、NumPyと多次元配列を使用しているためです。これらの多次元配列は「テンソル」とも呼ばれます。フレームワークは、ディープニューラルネットワークの操作をサポートします。これは非常にスケーラブルであり、多くの一般的なデータセットが付属しています。 GPU計算を使用し、リソ