如何使用Python將文字資料嵌入到維度向量中?
Tensorflow是谷歌提供的機器學習框架。它是一個開源框架,與Python結合使用,用於實現演算法、深度學習應用程式等等。它用於研究和生產目的。
Keras是作為ONEIROS專案(開放式神經電子智慧機器人作業系統)研究的一部分開發的。Keras是一個深度學習API,是用Python編寫的。它是一個高階API,具有高效的介面,有助於解決機器學習問題。它執行在Tensorflow框架之上。它旨在幫助快速進行實驗。它提供了開發和封裝機器學習解決方案所必需的基本抽象和構建塊。
Keras已存在於Tensorflow包中。可以使用以下程式碼行訪問它。
import tensorflow from tensorflow import keras
Keras函式式API有助於建立比使用順序API建立的模型更靈活的模型。函式式API可以處理具有非線性拓撲的模型,可以共享層,並可以處理多個輸入和輸出。深度學習模型通常是一個包含多個層的定向無環圖 (DAG)。函式式API有助於構建圖層圖。
我們正在使用Google Colaboratory執行以下程式碼。Google Colab或Colaboratory有助於在瀏覽器上執行Python程式碼,無需任何配置,並可免費訪問GPU(圖形處理單元)。Colaboratory構建在Jupyter Notebook之上。以下是我們將標題中的每個單詞嵌入到64維向量中的程式碼片段:
示例
print("Number of unique issue tags")
num_tags = 12
print("Size of vocabulary while preprocessing text data")
num_words = 10000
print("Number of classes for predictions")
num_classes = 4
title_input = keras.Input(
shape=(None,), name="title"
)
print("Variable length int sequence")
body_input = keras.Input(shape=(None,), name="body")
tags_input = keras.Input(
shape=(num_tags,), name="tags"
)
print("Embed every word in the title to a 64-dimensional vector")
title_features = layers.Embedding(num_words, 64)(title_input)
print("Embed every word into a 64-dimensional vector")
body_features = layers.Embedding(num_words, 64)(body_input)
print("Reduce sequence of embedded words into single 128-dimensional vector")
title_features = layers.LSTM(128)(title_features)
print("Reduce sequence of embedded words into single 132-dimensional vector")
body_features = layers.LSTM(32)(body_features)
print("Merge available features into a single vector by concatenating it")
x = layers.concatenate([title_features, body_features, tags_input])
print("Use logistic regression to predict the features")
priority_pred = layers.Dense(1, name="priority")(x)
department_pred = layers.Dense(num_classes, name="class")(x)
print("Instantiate a model that predicts priority and class")
model = keras.Model(
inputs=[title_input, body_input, tags_input],
outputs=[priority_pred, department_pred],
)程式碼來源 − https://www.tensorflow.org/guide/keras/functional
輸出
Number of unique issue tags Size of vocabulary while preprocessing text data Number of classes for predictions Variable length int sequence Embed every word in the title to a 64-dimensional vector Embed every word into a 64-dimensional vector Reduce sequence of embedded words into single 128-dimensional vector Reduce sequence of embedded words into single 132-dimensional vector Merge available features into a single vector by concatenating it Use logistic regression to predict the features Instantiate a model that predicts priority and class
解釋
函式式API可用於處理多個輸入和輸出。
順序API無法做到這一點。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP