Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python을 사용하여 Illiad 데이터 세트를 다운로드하고 탐색하는 데 Tensorflow를 어떻게 사용할 수 있습니까?

<시간/>

Tensorflow는 Google에서 제공하는 기계 학습 프레임워크입니다. 알고리즘, 딥 러닝 애플리케이션 등을 구현하기 위해 Python과 함께 사용되는 오픈 소스 프레임워크입니다. 연구 및 생산 목적으로 사용됩니다.

'tensorflow' 패키지는 아래 코드 줄을 사용하여 Windows에 설치할 수 있습니다. -

pip install tensorflow

Tensor는 TensorFlow에서 사용되는 데이터 구조입니다. 흐름도에서 가장자리를 연결하는 데 도움이 됩니다. 이 흐름도를 '데이터 흐름 그래프'라고 합니다. 텐서는 다차원 배열 또는 목록에 불과합니다.

세 가지 주요 속성을 사용하여 식별할 수 있습니다.

  • 순위 - 텐서의 차원에 대해 알려줍니다. 텐서의 순서 또는 정의된 텐서의 차원 수로 이해할 수 있습니다.

  • 유형 - Tensor의 요소와 관련된 데이터 유형에 대해 알려줍니다. 1차원, 2차원 또는 n차원 텐서일 수 있습니다.

  • 모양 - 행과 열의 개수입니다.

William Cowper, Edward(Earl of Derby), Samuel Butler의 세 번역 작업에 대한 텍스트 데이터가 포함된 Illiad의 데이터 세트를 사용할 것입니다. 모델은 한 줄의 텍스트가 제공될 때 번역자를 식별하도록 훈련됩니다. 사용된 텍스트 파일은 전처리되었습니다. 여기에는 문서 머리글 및 바닥글, 줄 번호 및 장 제목 제거가 포함됩니다.

Google Colaboratory를 사용하여 아래 코드를 실행하고 있습니다. Google Colab 또는 Colaboratory는 브라우저를 통해 Python 코드를 실행하는 데 도움이 되며 구성이 필요 없고 GPU(그래픽 처리 장치)에 대한 무료 액세스가 필요합니다. Collaboratory는 Jupyter Notebook 위에 구축되었습니다. 다음은 코드 조각입니다 -

예시

print("Loading the Illiad dataset")
DIRECTORY_URL = 'https://storage.googleapis.com/download.tensorflow.org/data/illiad/'
FILE_NAMES = ['cowper.txt', 'derby.txt', 'butler.txt']

print("Iterating through the name of the files")
for name in FILE_NAMES:
   text_dir = utils.get_file(name, origin=DIRECTORY_URL + name)

parent_dir = pathlib.Path(text_dir).parent
print("The list of files in the directory")
print(list(parent_dir.iterdir()))

코드 크레딧 - https://www.tensorflow.org/tutorials/load_data/text

출력

Loading the Illiad dataset
Iterating through the name of the files
Downloading data from
https://storage.googleapis.com/download.tensorflow.org/data/illiad/cowper.txt
819200/815980 [==============================] - 0s 0us/step
Downloading data from
https://storage.googleapis.com/download.tensorflow.org/data/illiad/derby.txt
811008/809730 [==============================] - 0s 0us/step
Downloading data from
https://storage.googleapis.com/download.tensorflow.org/data/illiad/butler.txt
811008/807992 [==============================] - 0s 0us/step
The list of files in the directory
[PosixPath('/root/.keras/datasets/derby.txt'), PosixPath('/root/.keras/datasets/cowper.txt'),
PosixPath('/root/.keras/datasets/butler.txt')]
[ ]

설명

  • 'tf.data.TextLineDataset'은 텍스트 파일에서 예제를 로드하는 데 사용됩니다.

  • 'tf.text'는 데이터를 전처리하는 데 사용됩니다.

  • 먼저 데이터 세트를 다운로드하고 탐색합니다.