0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AI技術を支える数学

Last updated at Posted at 2024-10-14
import re
from bs4 import BeautifulSoup
import MeCab
from sklearn.feature_extraction.text import CountVectorizer

# 1. HTMLタグを削除し、不要なスペースを除去する関数
def clean_text(text):
    # BeautifulSoupを使ってHTMLタグを削除
    text = BeautifulSoup(text, "html.parser").get_text()
    # 正規表現を使って不要なスペースを削除
    text = re.sub(r'\s+', ' ', text).strip()
    return text

# 2. MeCabを用いて日本語テキストを単語に分割
def tokenize_with_mecab(text):
    mecab = MeCab.Tagger('-Owakati')
    # MeCabで形態素解析し、単語に分割された文字列を返す
    tokenized_text = mecab.parse(text).strip()
    return tokenized_text

# 3. 正規化 (すべて小文字に変換)
def normalize_text(text):
    return text.lower()

# 4. CountVectorizerを使ってベクトル化
def vectorize_text(tokenized_texts):
    vectorizer = CountVectorizer()
    # 文字列のリストを受け取り、ベクトル化
    vectorized = vectorizer.fit_transform(tokenized_texts)
    return vectorized, vectorizer.get_feature_names_out()

# 実際のテキスト処理
raw_text = "<p>こんにちは、これはテストのHTMLタグを含んだテキストです。</p>"

# テキストのクレンジング
cleaned_text = clean_text(raw_text)
print("Cleaned Text:", cleaned_text)

# MeCabで形態素解析 (単語に分割)
tokenized_text = tokenize_with_mecab(cleaned_text)
print("Tokenized Text:", tokenized_text)

# 小文字に正規化
normalized_text = normalize_text(tokenized_text)
print("Normalized Text:", normalized_text)

# ベクトル化
vectorized_text, feature_names = vectorize_text([normalized_text])
print("Vectorized Text:\n", vectorized_text.toarray())
print("Feature Names:", feature_names)


import numpy as np
import matplotlib.pyplot as plt

# 逆文書頻度 (IDF) を計算する関数
def calculate_idf(N, Nt):
    return np.log(N / Nt)

# 文書全体の数 (N)
N = 1000

# 索引語が含まれる文書の数 (Nt) の範囲
Nt_values = np.arange(1, 101)  # 1から100までの文書数

# 各Ntに対するIDF値を計算
idf_values = calculate_idf(N, Nt_values)

# グラフの描画
plt.figure(figsize=(8, 6))
plt.plot(Nt_values, idf_values, marker='o')
plt.title('IDF vs. Number of Documents (Nt)')
plt.xlabel('Nt (Number of Documents containing the term)')
plt.ylabel('IDF (Inverse Document Frequency)')
plt.grid(True)
plt.show()

import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

# サンプル文書のベクトル化(埋め込みの模倣)
documents = {
    "文書1": np.array([0.1, 0.3, 0.7]),
    "文書2": np.array([0.2, 0.1, 0.3]),
    "文書3": np.array([0.6, 0.3, 0.4])
}

# 質問文のベクトル化(埋め込みの模倣)
query = np.array([0.2, 0.4, 0.9])

# コサイン類似度を計算する関数
def calculate_similarity(query_vector, document_vectors):
    similarities = {}
    for doc, vector in document_vectors.items():
        similarity = cosine_similarity([query_vector], [vector])[0][0]
        similarities[doc] = similarity
    return similarities

# 質問文と各文書の類似度を計算
similarities = calculate_similarity(query, documents)

# 結果を表示
print("質問文と各文書のコサイン類似度:")
for doc, similarity in similarities.items():
    print(f"{doc}: {similarity:.4f}")

# 最も類似度の高い文書を取得
best_match = max(similarities, key=similarities.get)
print(f"最も類似度が高い文書: {best_match}")


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics.pairwise import cosine_similarity

# サンプルの評価行列 (ユーザー × アイテム)
ratings_matrix = np.array([
    [5, 3, 0, 1],
    [4, 0, 0, 1],
    [1, 1, 0, 5],
    [1, 0, 0, 4],
    [0, 1, 5, 4],
])

# ユーザー間のコサイン類似度を計算
user_similarity = cosine_similarity(ratings_matrix)

# アイテム間のコサイン類似度を計算
item_similarity = cosine_similarity(ratings_matrix.T)

# 評価行列の可視化
plt.figure(figsize=(10, 6))
sns.heatmap(ratings_matrix, annot=True, cmap="coolwarm", cbar=True, linewidths=0.5)
plt.title("Ratings Matrix (Users × Items)")
plt.xlabel("Items")
plt.ylabel("Users")
plt.show()

# ユーザー間の類似度の可視化
plt.figure(figsize=(8, 6))
sns.heatmap(user_similarity, annot=True, cmap="Blues", cbar=True, linewidths=0.5)
plt.title("User Similarity (Cosine Similarity)")
plt.xlabel("Users")
plt.ylabel("Users")
plt.show()

# アイテム間の類似度の可視化
plt.figure(figsize=(8, 6))
sns.heatmap(item_similarity, annot=True, cmap="Greens", cbar=True, linewidths=0.5)
plt.title("Item Similarity (Cosine Similarity)")
plt.xlabel("Items")
plt.ylabel("Items")
plt.show()


import numpy as np
import pandas as pd

# 評価値行列 R を作成する
# 未評価のアイテムは 0 とする
ratings_data = {
    "user1": [5, 3, 0, 1],  # user1の評価
    "user2": [4, 0, 0, 1],  # user2の評価
    "user3": [1, 1, 0, 5],  # user3の評価
    "user4": [1, 0, 0, 4],  # user4の評価
    "user5": [0, 1, 5, 4],  # user5の評価
}

# 行列として扱うため、DataFrameを使用
items = ['item1', 'item2', 'item3', 'item4']  # アイテム
ratings_matrix = pd.DataFrame(ratings_data, index=items).T

# 評価値行列 R を表示
print("評価値行列 R:")
print(ratings_matrix)

# 欠損値(未評価)は0として扱う
R = ratings_matrix.values

# 評価値行列の概要を表示
print("\n評価値行列 R (numpy array):")
print(R)

# 行と列のラベル(ユーザーとアイテム)を使った表記
for i, user in enumerate(ratings_matrix.index):
    for j, item in enumerate(ratings_matrix.columns):
        print(f"r_{i+1},{j+1} (user {user} の {item} に対する評価): {R[i, j]}")

import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import matplotlib.pyplot as plt

# ユーザーAとユーザーBの評価ベクトル(例: 1,2,3,4,5 と 5,4,3,2,1)
A = np.array([1, 2, 3, 4, 5])
B = np.array([5, 4, 3, 2, 1])

# 中心化(各評価値から平均値を引く)
A_mean = A.mean()
B_mean = B.mean()

A_centered = A - A_mean
B_centered = B - B_mean

# 中心化後のベクトル表示
print("中心化後のA:", A_centered)
print("中心化後のB:", B_centered)

# 中心化後のコサイン類似度の計算
similarity = cosine_similarity([A_centered], [B_centered])[0][0]
print("中心化後のコサイン類似度:", similarity)

# 中心化前後のベクトルを視覚化
fig, ax = plt.subplots(1, 2, figsize=(10, 4))

# 中心化前のベクトルプロット
ax[0].plot(A, label='User A', marker='o')
ax[0].plot(B, label='User B', marker='o')
ax[0].set_title('Original Vectors')
ax[0].legend()

# 中心化後のベクトルプロット
ax[1].plot(A_centered, label='User A (centered)', marker='o')
ax[1].plot(B_centered, label='User B (centered)', marker='o')
ax[1].set_title('Centered Vectors')
ax[1].legend()

plt.show()


import numpy as np
import matplotlib.pyplot as plt

# Generate sample data for a simple linear relationship: y = 2x + 1
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

# Initialize parameters (weights w1 and bias w0)
w1 = np.random.randn(1)
w0 = np.random.randn(1)
learning_rate = 0.01
iterations = 1000

# Prediction function f(x) = w0 + w1 * x
def predict(X, w0, w1):
    return w0 + w1 * X

# Loss function: Mean Squared Error (MSE)
def compute_loss(y, y_pred):
    return np.mean((y - y_pred) ** 2)

# Gradient Descent to update weights
loss_history = []

for i in range(iterations):
    # Compute the predictions
    y_pred = predict(X, w0, w1)
    
    # Compute the residuals
    residuals = y_pred - y
    
    # Compute gradients (partial derivatives)
    dw1 = 2 * np.mean(residuals * X)
    dw0 = 2 * np.mean(residuals)
    
    # Update parameters
    w1 -= learning_rate * dw1
    w0 -= learning_rate * dw0
    
    # Compute the loss and store it
    loss = compute_loss(y, y_pred)
    loss_history.append(loss)

# Plot the loss function over iterations
plt.plot(loss_history)
plt.title("Loss Function Over Iterations")
plt.xlabel("Iteration")
plt.ylabel("Loss (Mean Squared Error)")
plt.show()

# Plot the original data and the final model
plt.scatter(X, y, color='blue', label="Original data")
plt.plot(X, predict(X, w0, w1), color='red', label="Fitted line")
plt.title("Linear Regression Fit")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()

# Display final parameters
print(f"Final weight (w1): {w1}")
print(f"Final bias (w0): {w0}")


import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

# Load and preprocess the dataset (CIFAR-10 for image classification)
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

# Define the CNN model
model = models.Sequential()

# Convolutional layer 1
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))

# Convolutional layer 2
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))

# Convolutional layer 3
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# Flatten the data for the fully connected layer
model.add(layers.Flatten())

# Fully connected layer
model.add(layers.Dense(64, activation='relu'))

# Output layer with Softmax for classification
model.add(layers.Dense(10, activation='softmax'))

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train the CNN model
history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))

# Plot training and validation accuracy
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0, 1])
plt.legend(loc='lower right')
plt.show()

# Evaluate the model on the test set
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f"Test accuracy: {test_acc}")

import numpy as np
import matplotlib.pyplot as plt

# Generate some sample data: y = 2x + 1 with some noise
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 2 * X + 1 + np.random.randn(100, 1) * 0.1

# Initialize the parameters (weight and bias)
a = np.random.randn(1)
b = np.random.randn(1)
learning_rate = 0.01
iterations = 1000

# Function to compute the predicted value
def predict(X, a, b):
    return a * X + b

# Function to compute the loss (Mean Squared Error)
def compute_loss(y_true, y_pred):
    return np.mean((y_true - y_pred) ** 2)

# Gradient descent function to update the parameters
loss_history = []

for i in range(iterations):
    # Compute the predicted values
    y_pred = predict(X, a, b)
    
    # Compute the residuals (error)
    residuals = y_pred - y
    
    # Compute gradients for weight (a) and bias (b)
    da = 2 * np.mean(residuals * X)
    db = 2 * np.mean(residuals)
    
    # Update parameters
    a -= learning_rate * da
    b -= learning_rate * db
    
    # Compute and store the loss
    loss = compute_loss(y, y_pred)
    loss_history.append(loss)

# Plot the loss function over iterations
plt.plot(loss_history)
plt.title("Loss Function Over Iterations (Gradient Descent)")
plt.xlabel("Iteration")
plt.ylabel("Loss (Mean Squared Error)")
plt.show()

# Plot the original data and the fitted line
plt.scatter(X, y, color='blue', label='Original data')
plt.plot(X, predict(X, a, b), color='red', label='Fitted line')
plt.title("Linear Regression Fit")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()

# Display final parameters (weight and bias)
print(f"Final weight (a): {a}")
print(f"Final bias (b): {b}")



import numpy as np

# Sigmoid activation function and its derivative
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
    return x * (1 - x)

# Forward pass
def forward(X, weights1, weights2):
    z1 = np.dot(X, weights1)  # Hidden layer input
    a1 = sigmoid(z1)          # Hidden layer activation
    z2 = np.dot(a1, weights2)  # Output layer input
    a2 = sigmoid(z2)           # Output layer activation
    return a1, a2

# Backpropagation
def backward(X, y, a1, a2, weights1, weights2, learning_rate):
    # Compute output layer error
    output_error = a2 - y
    output_delta = output_error * sigmoid_derivative(a2)
    
    # Compute hidden layer error
    hidden_error = np.dot(output_delta, weights2.T)
    hidden_delta = hidden_error * sigmoid_derivative(a1)
    
    # Update weights
    weights2 -= np.dot(a1.T, output_delta) * learning_rate
    weights1 -= np.dot(X.T, hidden_delta) * learning_rate
    
    return weights1, weights2

# Training the neural network
def train(X, y, weights1, weights2, epochs, learning_rate):
    for epoch in range(epochs):
        # Forward pass
        a1, a2 = forward(X, weights1, weights2)
        
        # Backward pass
        weights1, weights2 = backward(X, y, a1, a2, weights1, weights2, learning_rate)
        
        # Compute and print loss at every 100 epochs
        if epoch % 100 == 0:
            loss = np.mean(np.square(y - a2))
            print(f"Epoch {epoch}, Loss: {loss}")
    
    return weights1, weights2

# Example dataset (XOR problem)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])  # Input features
y = np.array([[0], [1], [1], [0]])              # Target labels

# Initialize weights randomly
np.random.seed(42)
weights1 = np.random.rand(2, 3)  # Weights between input and hidden layer (2x3 matrix)
weights2 = np.random.rand(3, 1)  # Weights between hidden and output layer (3x1 matrix)

# Training parameters
learning_rate = 0.1
epochs = 1000

# Train the neural network
weights1, weights2 = train(X, y, weights1, weights2, epochs, learning_rate)

# Test the neural network after training
_, a2 = forward(X, weights1, weights2)
print("Predicted outputs after training:")
print(a2)

import numpy as np

# Softmax function implementation
def softmax(logits):
    exp_logits = np.exp(logits)
    return exp_logits / np.sum(exp_logits, axis=0)

# Example: Simulate transformer output logits for 10,000 tokens
np.random.seed(42)
vocab_size = 10000
logits = np.random.rand(vocab_size)  # Logits from the final layer before softmax

# Apply softmax to convert logits to probabilities
probabilities = softmax(logits)

# Select the token with the highest probability
predicted_token = np.argmax(probabilities)

# Print the results
print(f"Predicted token index: {predicted_token}")
print(f"Probability of the predicted token: {probabilities[predicted_token]:.4f}")

# Visualizing the top 10 most probable tokens
top_indices = np.argsort(probabilities)[-10:][::-1]
print("\nTop 10 token indices and their probabilities:")
for idx in top_indices:
    print(f"Token index: {idx}, Probability: {probabilities[idx]:.4f}")

from sklearn.feature_extraction.text import CountVectorizer
import numpy as np

# Sample text
text = "Mount Fuji looks beautiful in spring."

# Tokenization: Splitting text into words (tokens)
vectorizer = CountVectorizer()
tokenized = vectorizer.fit_transform([text])

# Get token (word) to ID mapping
token_to_id = vectorizer.vocabulary_

# Example output: tokenized words and their IDs
print("Tokenized words and their IDs:")
for word, idx in token_to_id.items():
    print(f"'{word}': {idx}")

# Simulate word embeddings by generating random vectors for each token
embedding_dim = 300  # Example dimension for word embeddings
embedding_matrix = {word: np.random.rand(embedding_dim) for word in token_to_id}

# Get the embedding for each token
print("\nWord embeddings (simulated):")
for word, idx in token_to_id.items():
    print(f"Embedding for '{word}' (first 5 dimensions): {embedding_matrix[word][:5]}")


import numpy as np
import matplotlib.pyplot as plt

# Define a function to generate positional encodings
def positional_encoding(max_len, d_model):
    # Initialize a matrix of zeros with shape (max_len, d_model)
    PE = np.zeros((max_len, d_model))
    
    # Calculate positional encodings
    for pos in range(max_len):
        for i in range(0, d_model, 2):
            PE[pos, i] = np.sin(pos / (10000 ** (i / d_model)))
            if i + 1 < d_model:
                PE[pos, i + 1] = np.cos(pos / (10000 ** (i / d_model)))
    
    return PE

# Parameters
max_len = 10  # Maximum length of the input sequence
d_model = 6   # Dimension of the model (embedding dimension)

# Generate positional encodings
PE = positional_encoding(max_len, d_model)

# Visualize the positional encoding
plt.figure(figsize=(10, 6))
plt.title("Positional Encoding")
plt.pcolormesh(PE, cmap='viridis')
plt.xlabel('Embedding Dimension')
plt.ylabel('Token Position')
plt.colorbar()
plt.show()

# Print the positional encodings for each token position
print("Positional Encodings:")
print(PE)


import numpy as np
import matplotlib.pyplot as plt

# Function to generate positional encodings
def positional_encoding(max_len, d_model):
    PE = np.zeros((max_len, d_model))
    for pos in range(max_len):
        for i in range(0, d_model, 2):
            PE[pos, i] = np.sin(pos / (10000 ** (i / d_model)))
            if i + 1 < d_model:
                PE[pos, i + 1] = np.cos(pos / (10000 ** ((i) / d_model)))
    return PE

# Parameters
max_len = 10  # Maximum length of input sequence
d_model = 6   # Dimension of the embedding

# Generate positional encodings
PE = positional_encoding(max_len, d_model)

# Display positional encoding
print("Positional Encoding matrix:")
print(PE)

# Visualize the positional encodings
plt.figure(figsize=(10, 6))
plt.pcolormesh(PE, cmap='viridis')
plt.xlabel('Embedding Dimension')
plt.ylabel('Token Position')
plt.title('Positional Encoding')
plt.colorbar()
plt.show()


import re
from bs4 import BeautifulSoup
import spacy
from sklearn.feature_extraction.text import CountVectorizer

# 1. クレンジング: HTMLタグの除去とスペース削除
def clean_text(html_text):
    # BeautifulSoupを使ってHTMLタグを除去
    soup = BeautifulSoup(html_text, "lxml")
    text = soup.get_text()
    
    # 正規表現で余分なスペースを削除
    text = re.sub(r'\s+', ' ', text).strip()
    return text

# 2. 単語分割: spaCyを使って日本語の形態素解析
def tokenize(text):
    nlp = spacy.blank("ja")
    nlp.add_pipe("sudachipy_tokenizer")
    doc = nlp(text)
    tokens = [token.text.lower() for token in doc]  # 小文字化して単語リストを返す
    return tokens

# 3. 正規化: 小文字に統一 (上記 tokenize 関数内で実行済み)

# 4. ベクトル化: CountVectorizerを使ってOne-hot encodingのようにベクトル化
def vectorize(tokens):
    vectorizer = CountVectorizer(tokenizer=lambda x: x, lowercase=False)
    X = vectorizer.fit_transform([tokens])  # 1文に対してのベクトル化
    return X.toarray(), vectorizer.get_feature_names_out()

# テスト用のHTMLテキスト
html_text = """
<html><body>
<h1>タイトル</h1>
<p>これはテストの文章です。<br>彼は犬に追われていた。</p>
</body></html>
"""

# データ処理の流れ
cleaned_text = clean_text(html_text)
tokens = tokenize(cleaned_text)
vectorized_data, feature_names = vectorize(tokens)

# 結果を表示
print("クレンジング後のテキスト:", cleaned_text)
print("単語分割後のトークン:", tokens)
print("ベクトル化結果:", vectorized_data)
print("ベクトル化に対応する単語リスト:", feature_names)


import numpy as np

# Matrix Factorization using Stochastic Gradient Descent (SGD)
class MatrixFactorization:
    def __init__(self, R, K, alpha, beta, iterations):
        """
        R: User-Item Rating Matrix
        K: Number of latent dimensions
        alpha: Learning rate
        beta: Regularization parameter
        iterations: Number of iterations
        """
        self.R = R
        self.num_users, self.num_items = R.shape
        self.K = K
        self.alpha = alpha
        self.beta = beta
        self.iterations = iterations

    def train(self):
        # Initialize latent factor matrices P and Q (with random values)
        self.P = np.random.normal(scale=1./self.K, size=(self.num_users, self.K))
        self.Q = np.random.normal(scale=1./self.K, size=(self.num_items, self.K))
        
        # Training process with Stochastic Gradient Descent
        self.b_u = np.zeros(self.num_users)  # User bias
        self.b_i = np.zeros(self.num_items)  # Item bias
        self.b = np.mean(self.R[np.where(self.R != 0)])  # Global bias
        
        # List of training losses to monitor convergence
        self.training_process = []
        
        # Iterate over each step
        for i in range(self.iterations):
            for u in range(self.num_users):
                for i in range(self.num_items):
                    if self.R[u, i] > 0:  # Only process non-zero ratings
                        error = self.R[u, i] - self.predict(u, i)
                        # Update biases and latent factors using gradient descent
                        self.b_u[u] += self.alpha * (error - self.beta * self.b_u[u])
                        self.b_i[i] += self.alpha * (error - self.beta * self.b_i[i])
                        self.P[u, :] += self.alpha * (error * self.Q[i, :] - self.beta * self.P[u, :])
                        self.Q[i, :] += self.alpha * (error * self.P[u, :] - self.beta * self.Q[i, :])
            
            # Compute the training loss
            loss = self.mse()
            self.training_process.append((i, loss))
            if (i+1) % 10 == 0:
                print(f"Iteration: {i+1}; Error: {loss}")

    def predict(self, u, i):
        """Predict the rating of user u for item i"""
        return self.b + self.b_u[u] + self.b_i[i] + self.P[u, :].dot(self.Q[i, :].T)

    def mse(self):
        """Mean Squared Error of predictions"""
        xs, ys = self.R.nonzero()
        predicted = self.full_matrix()
        error = 0
        for x, y in zip(xs, ys):
            error += (self.R[x, y] - predicted[x, y]) ** 2
        return np.sqrt(error / len(xs))

    def full_matrix(self):
        """Compute the full matrix with predictions for all user-item pairs"""
        return self.b + self.b_u[:, np.newaxis] + self.b_i[np.newaxis:, ] + self.P.dot(self.Q.T)

# User-Item Rating Matrix (0 represents missing ratings)
R = np.array([[5, 3, 0, 1],
              [4, 0, 0, 1],
              [1, 1, 0, 5],
              [1, 0, 0, 4],
              [0, 1, 5, 4]])

# Hyperparameters
K = 2  # Number of latent dimensions
alpha = 0.01  # Learning rate
beta = 0.01  # Regularization parameter
iterations = 100  # Number of iterations

# Initialize and train Matrix Factorization model
mf = MatrixFactorization(R, K, alpha, beta, iterations)
mf.train()

# Display the completed matrix
print("\nPredicted Rating Matrix:")
print(mf.full_matrix())

import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

# 評価行列 (ユーザー×アイテム) - 欠損値は0で表す
R = np.array([[5, 3, 0, 1],
              [4, 0, 0, 1],
              [1, 1, 0, 5],
              [1, 0, 0, 4],
              [0, 1, 5, 4]])

def predict_missing_value(R, target_user, target_item):
    """
    コサイン類似度に基づいて欠損値を推定する
    :param R: 評価行列 (ユーザー×アイテム)
    :param target_user: 欠損値を推定するユーザーのインデックス
    :param target_item: 欠損値を推定するアイテムのインデックス
    :return: 推定された評価値
    """
    
    # 評価が存在しない(0の)部分は無視するために、欠損値を0以外のNoneに置き換える
    R_nonzero = np.where(R != 0, R, np.nan)
    
    # 1. コサイン類似度をユーザー間で計算
    user_similarity = cosine_similarity(np.nan_to_num(R_nonzero))
    
    # 2. 対象ユーザーと他ユーザーの類似度を取得
    similarities = user_similarity[target_user]
    
    # 3. 対象アイテムについて評価しているユーザーのインデックスを取得
    nonzero_user_indices = np.where(R[:, target_item] != 0)[0]
    
    if len(nonzero_user_indices) == 0:
        return 0  # 他のユーザーが評価していない場合、推定できないため0を返す
    
    # 4. 類似度に基づいた加重平均で欠損値を推定
    weighted_sum = 0
    sum_of_weights = 0
    
    for user_idx in nonzero_user_indices:
        weight = similarities[user_idx]
        rating = R[user_idx, target_item]
        weighted_sum += weight * rating
        sum_of_weights += abs(weight)
    
    if sum_of_weights == 0:
        return 0  # 類似度が全くない場合、推定できないため0を返す
    
    predicted_value = weighted_sum / sum_of_weights
    return predicted_value

# 欠損値の推定例 (user3, item1 の評価を推定)
target_user = 2  # user3
target_item = 0  # item1

predicted_rating = predict_missing_value(R, target_user, target_item)
print(f"推定された評価値 (user{target_user+1}, item{target_item+1}): {predicted_rating:.2f}")

import numpy as np

class MatrixFactorization:
    def __init__(self, R, K, alpha, beta, iterations):
        """
        R: 評価行列 (ユーザー×アイテム)
        K: 因子の次元数
        alpha: 学習率
        beta: 正則化パラメータ
        iterations: 更新回数
        """
        self.R = R
        self.num_users, self.num_items = R.shape
        self.K = K
        self.alpha = alpha
        self.beta = beta
        self.iterations = iterations

    def train(self):
        # ユーザー因子行列 P と アイテム因子行列 Q をランダムに初期化
        self.P = np.random.normal(scale=1./self.K, size=(self.num_users, self.K))
        self.Q = np.random.normal(scale=1./self.K, size=(self.num_items, self.K))

        # 学習プロセス: 勾配降下法による更新を繰り返す
        for iteration in range(self.iterations):
            for u in range(self.num_users):
                for i in range(self.num_items):
                    if self.R[u, i] > 0:  # 実際の評価が存在する箇所のみ処理
                        # 誤差の計算
                        error = self.R[u, i] - self.predict(u, i)

                        # P_u と Q_i の更新 (勾配降下法)
                        self.P[u, :] += self.alpha * (error * self.Q[i, :] - self.beta * self.P[u, :])
                        self.Q[i, :] += self.alpha * (error * self.P[u, :] - self.beta * self.Q[i, :])

            # 損失関数の計算
            loss = self.loss()
            if (iteration+1) % 10 == 0:
                print(f"Iteration: {iteration+1}; Loss: {loss:.4f}")

    def predict(self, u, i):
        """ユーザーuがアイテムiに対して予測する評価値"""
        return np.dot(self.P[u, :], self.Q[i, :].T)

    def loss(self):
        """損失関数 (残差 + 正則化項) の計算"""
        xs, ys = self.R.nonzero()  # 実際に評価が行われている要素のみ対象
        predicted = self.full_matrix()
        error = 0
        for x, y in zip(xs, ys):
            error += (self.R[x, y] - predicted[x, y]) ** 2
        # 正則化項を加える
        error += self.beta * (np.linalg.norm(self.P) + np.linalg.norm(self.Q))
        return np.sqrt(error)

    def full_matrix(self):
        """ユーザー因子行列 P と アイテム因子行列 Q から完全な予測評価行列を生成"""
        return np.dot(self.P, self.Q.T)

# 評価行列 (ユーザー×アイテム) - 0は評価が行われていない箇所
R = np.array([[5, 3, 0, 1],
              [4, 0, 0, 1],
              [1, 1, 0, 5],
              [1, 0, 0, 4],
              [0, 1, 5, 4]])

# パラメータの設定
K = 2  # 因子の次元数
alpha = 0.01  # 学習率
beta = 0.01  # 正則化パラメータ
iterations = 100  # 学習の反復回数

# Matrix Factorizationのモデルを構築して学習
mf = MatrixFactorization(R, K, alpha, beta, iterations)
mf.train()

# 予測結果の評価行列を表示
print("\n予測評価行列:")
print(mf.full_matrix())


import numpy as np

# データ生成
np.random.seed(42)
X = 2 * np.random.rand(100, 1)  # 100個のランダムなデータ点 (特徴量)
y = 4 + 3 * X + np.random.randn(100, 1)  # 実際の値 (正解値)  w=3, b=4 を使って生成

# モデルの初期化
w = np.random.randn(1)  # 重みの初期値
b = np.random.randn(1)  # バイアスの初期値
alpha = 0.01  # 学習率
iterations = 1000  # 更新の回数

# 予測関数
def predict(X, w, b):
    return X * w + b

# 損失関数 (平均二乗誤差)
def compute_loss(y, y_pred):
    return np.mean((y - y_pred) ** 2)

# 勾配降下法によるパラメータ更新
def update_parameters(X, y, y_pred, w, b, alpha):
    m = len(y)
    dw = -(2/m) * np.sum((y - y_pred) * X)  # 重み w に対する勾配
    db = -(2/m) * np.sum(y - y_pred)  # バイアス b に対する勾配
    w = w - alpha * dw  # 重みの更新
    b = b - alpha * db  # バイアスの更新
    return w, b

# 学習プロセス
for i in range(iterations):
    # 1. 予測値を計算
    y_pred = predict(X, w, b)
    
    # 2. 損失関数を計算
    loss = compute_loss(y, y_pred)
    
    # 3. パラメータを更新
    w, b = update_parameters(X, y, y_pred, w, b, alpha)
    
    # 進捗の表示 (10回ごとに表示)
    if i % 100 == 0:
        print(f"Iteration {i}, Loss: {loss:.4f}, w: {w[0]:.4f}, b: {b[0]:.4f}")

# 結果の表示
print("\n最終的なパラメータ:")
print(f"w (重み): {w[0]:.4f}")
print(f"b (バイアス): {b[0]:.4f}")



# 必要なライブラリをインポート
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

# MNISTデータセットのロード
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()

# データの前処理
train_images = train_images.reshape((train_images.shape[0], 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((test_images.shape[0], 28, 28, 1)).astype('float32') / 255

# CNNモデルの構築
model = models.Sequential()

# 畳み込み層1(32個のフィルタ、カーネルサイズ: 3x3、ReLU活性化関数)
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))

# 畳み込み層2(64個のフィルタ、カーネルサイズ: 3x3、ReLU活性化関数)
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))

# 畳み込み層3(64個のフィルタ、カーネルサイズ: 3x3、ReLU活性化関数)
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# 全結合層にデータを渡すために1次元にフラット化
model.add(layers.Flatten())

# 全結合層(Dense layer、64ユニット、ReLU活性化関数)
model.add(layers.Dense(64, activation='relu'))

# 出力層(10クラス分類のためのソフトマックス関数)
model.add(layers.Dense(10, activation='softmax'))

# モデルの構成
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# モデルの概要を表示
model.summary()

# モデルの訓練
history = model.fit(train_images, train_labels, epochs=5, 
                    validation_data=(test_images, test_labels))

# モデルの評価
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f'\nテスト精度: {test_acc:.4f}')

# 訓練過程の可視化(損失値と精度の推移)
plt.plot(history.history['accuracy'], label='訓練精度')
plt.plot(history.history['val_accuracy'], label='検証精度')
plt.xlabel('エポック')
plt.ylabel('精度')
plt.legend(loc='lower right')
plt.show()

import numpy as np

# ソフトマックス関数
def softmax(z):
    exp_z = np.exp(z - np.max(z))  # オーバーフロー防止のため最大値を引く
    return exp_z / np.sum(exp_z, axis=1, keepdims=True)

# 交差エントロピー損失関数
def cross_entropy_loss(y_true, y_pred):
    n_samples = y_true.shape[0]
    logp = - np.log(y_pred[range(n_samples), y_true])
    loss = np.sum(logp) / n_samples
    return loss

# データの作成 (特徴量と正解ラベル)
np.random.seed(42)
X = np.random.randn(100, 3)  # 100サンプル、3特徴量
y = np.random.randint(0, 3, 100)  # 3クラスのラベル (0, 1, 2)

# 重みの初期化
n_features = X.shape[1]
n_classes = len(np.unique(y))
W = np.random.randn(n_features, n_classes)  # 重みパラメータ
b = np.zeros((1, n_classes))  # バイアス

# 学習率とエポック数の設定
alpha = 0.01  # 学習率
n_epochs = 1000  # エポック数

# 学習ループ
for epoch in range(n_epochs):
    # 1. 線形関数 (z = XW + b)
    z = np.dot(X, W) + b
    
    # 2. ソフトマックス関数で確率に変換
    y_pred = softmax(z)
    
    # 3. 損失関数の計算 (交差エントロピー)
    loss = cross_entropy_loss(y, y_pred)
    
    # 4. 勾配の計算
    n_samples = X.shape[0]
    y_pred[range(n_samples), y] -= 1  # 正解ラベルに対する誤差
    dW = np.dot(X.T, y_pred) / n_samples  # 重みに対する勾配
    db = np.sum(y_pred, axis=0, keepdims=True) / n_samples  # バイアスに対する勾配
    
    # 5. パラメータの更新
    W -= alpha * dW
    b -= alpha * db

    # 10エポックごとに損失を表示
    if (epoch + 1) % 100 == 0:
        print(f'Epoch {epoch+1}/{n_epochs}, Loss: {loss:.4f}')

# 学習完了後の最終損失
print(f'最終損失: {loss:.4f}')



import numpy as np

# シグモイド関数とその微分
def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def sigmoid_derivative(z):
    return sigmoid(z) * (1 - sigmoid(z))

# ソフトマックス関数
def softmax(z):
    exp_z = np.exp(z - np.max(z, axis=1, keepdims=True))
    return exp_z / np.sum(exp_z, axis=1, keepdims=True)

# 交差エントロピー損失関数
def cross_entropy_loss(y_true, y_pred):
    n_samples = y_true.shape[0]
    logp = - np.log(y_pred[range(n_samples), y_true])
    loss = np.sum(logp) / n_samples
    return loss

# データ生成
np.random.seed(42)
X = np.random.randn(100, 3)  # 100サンプル、3特徴量
y = np.random.randint(0, 3, 100)  # 3クラスのラベル (0, 1, 2)

# パラメータの初期化
n_features = X.shape[1]
n_hidden = 5  # 隠れ層のユニット数
n_classes = 3  # 出力クラス数
alpha = 0.01  # 学習率
epochs = 1000  # エポック数

# 重みとバイアスの初期化
W1 = np.random.randn(n_features, n_hidden)
b1 = np.zeros((1, n_hidden))
W2 = np.random.randn(n_hidden, n_classes)
b2 = np.zeros((1, n_classes))

# 学習プロセス
for epoch in range(epochs):
    # 順伝播
    z1 = np.dot(X, W1) + b1
    a1 = sigmoid(z1)
    z2 = np.dot(a1, W2) + b2
    a2 = softmax(z2)
    
    # 損失関数の計算
    loss = cross_entropy_loss(y, a2)
    
    # 逆伝播(誤差の計算)
    delta2 = a2
    delta2[range(X.shape[0]), y] -= 1  # 出力層の誤差
    dW2 = np.dot(a1.T, delta2) / X.shape[0]  # W2 の勾配
    db2 = np.sum(delta2, axis=0, keepdims=True) / X.shape[0]  # b2 の勾配
    
    delta1 = np.dot(delta2, W2.T) * sigmoid_derivative(z1)  # 隠れ層の誤差
    dW1 = np.dot(X.T, delta1) / X.shape[0]  # W1 の勾配
    db1 = np.sum(delta1, axis=0, keepdims=True) / X.shape[0]  # b1 の勾配
    
    # パラメータの更新
    W1 -= alpha * dW1
    b1 -= alpha * db1
    W2 -= alpha * dW2
    b2 -= alpha * db2
    
    # 100エポックごとに損失を表示
    if (epoch + 1) % 100 == 0:
        print(f'Epoch {epoch+1}/{epochs}, Loss: {loss:.4f}')

# 学習終了後の損失表示
print(f'最終損失: {loss:.4f}')

from transformers import GPT2LMHeadModel, GPT2Tokenizer

# モデルとトークナイザのロード (GPT-2)
model_name = "gpt2"
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

# モデルに入力するテキストの準備
input_text = "Once upon a time, in a land far, far away,"

# 入力テキストをトークン化
input_ids = tokenizer.encode(input_text, return_tensors="pt")

# テキスト生成
output = model.generate(
    input_ids, 
    max_length=100,  # 生成するテキストの最大長
    num_return_sequences=1,  # 生成するテキストの数
    no_repeat_ngram_size=2,  # n-gramの繰り返しを防ぐ
    top_k=50,  # トップKサンプリング
    top_p=0.95,  # トップPサンプリング (核サンプリング)
    temperature=0.7  # 温度パラメータ (テキストの多様性)
)

# 生成されたテキストをデコードして表示
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print(generated_text)


import numpy as np

# シグモイド関数とその導関数
def sigmoid(z):
    return 1 / (1 + np.exp(-z))

# ソフトマックス関数
def softmax(z):
    exp_z = np.exp(z - np.max(z, axis=1, keepdims=True))
    return exp_z / np.sum(exp_z, axis=1, keepdims=True)

# 交差エントロピー損失関数
def cross_entropy_loss(y_true, y_pred):
    n_samples = y_true.shape[0]
    logp = - np.log(y_pred[range(n_samples), y_true])
    loss = np.sum(logp) / n_samples
    return loss

# データ生成 (2クラスの分類問題)
np.random.seed(42)
X = np.random.randn(100, 3)  # 100サンプル、3特徴量
y = np.random.randint(0, 2, 100)  # 2クラスのラベル (0, 1)

# パラメータの初期化
n_features = X.shape[1]
n_hidden = 5  # 隠れ層のユニット数
n_classes = 2  # 出力クラス数
alpha = 0.01  # 学習率
epochs = 1000  # エポック数

# 重みとバイアスの初期化
W1 = np.random.randn(n_features, n_hidden)
b1 = np.zeros((1, n_hidden))
W2 = np.random.randn(n_hidden, n_classes)
b2 = np.zeros((1, n_classes))

# 学習プロセス
for epoch in range(epochs):
    # 順伝播
    z1 = np.dot(X, W1) + b1
    a1 = sigmoid(z1)
    z2 = np.dot(a1, W2) + b2
    a2 = softmax(z2)
    
    # 損失関数の計算
    loss = cross_entropy_loss(y, a2)
    
    # 逆伝播(誤差の計算)
    delta2 = a2
    delta2[range(X.shape[0]), y] -= 1  # 出力層の誤差
    dW2 = np.dot(a1.T, delta2) / X.shape[0]  # W2 の勾配
    db2 = np.sum(delta2, axis=0, keepdims=True) / X.shape[0]  # b2 の勾配
    
    delta1 = np.dot(delta2, W2.T) * (a1 * (1 - a1))  # 隠れ層の誤差
    dW1 = np.dot(X.T, delta1) / X.shape[0]  # W1 の勾配
    db1 = np.sum(delta1, axis=0, keepdims=True) / X.shape[0]  # b1 の勾配
    
    # パラメータの更新
    W1 -= alpha * dW1
    b1 -= alpha * db1
    W2 -= alpha * dW2
    b2 -= alpha * db2
    
    # 100エポックごとに損失を表示
    if (epoch + 1) % 100 == 0:
        print(f'Epoch {epoch+1}/{epochs}, Loss: {loss:.4f}')

# テストデータで予測を実行
def predict(X, W1, b1, W2, b2):
    z1 = np.dot(X, W1) + b1
    a1 = sigmoid(z1)
    z2 = np.dot(a1, W2) + b2
    a2 = softmax(z2)
    return np.argmax(a2, axis=1)

# テストデータで予測
y_pred = predict(X, W1, b1, W2, b2)

# 結果の表示
print("\n予測ラベル:", y_pred[:10])
print("実際のラベル:", y[:10])


import numpy as np

# ソフトマックス関数の実装
def softmax(z):
    exp_z = np.exp(z - np.max(z, axis=1, keepdims=True))  # オーバーフロー対策
    return exp_z / np.sum(exp_z, axis=1, keepdims=True)

# 交差エントロピー損失関数
def cross_entropy_loss(y_true, y_pred):
    n_samples = y_true.shape[0]
    logp = - np.log(y_pred[range(n_samples), y_true])
    loss = np.sum(logp) / n_samples
    return loss

# データセットの準備(サンプルデータ)
np.random.seed(42)
X = np.random.randn(100, 3)  # 100サンプル、3特徴量
y = np.random.randint(0, 3, 100)  # 3クラスのラベル (0, 1, 2)

# 重みパラメータの初期化
n_features = X.shape[1]
n_classes = len(np.unique(y))
W = np.random.randn(n_features, n_classes)  # 重みパラメータ
b = np.zeros((1, n_classes))  # バイアス

# 学習率とエポック数の設定
alpha = 0.01  # 学習率
epochs = 1000  # エポック数

# 学習ループ
for epoch in range(epochs):
    # 1. 線形変換(z = XW + b)
    z = np.dot(X, W) + b
    
    # 2. ソフトマックス関数で確率を計算
    y_pred = softmax(z)
    
    # 3. 損失関数の計算(交差エントロピー)
    loss = cross_entropy_loss(y, y_pred)
    
    # 4. 勾配の計算
    n_samples = X.shape[0]
    y_pred[range(n_samples), y] -= 1  # 正解クラスに対する誤差
    dW = np.dot(X.T, y_pred) / n_samples  # 重みに対する勾配
    db = np.sum(y_pred, axis=0, keepdims=True) / n_samples  # バイアスに対する勾配
    
    # 5. 重みとバイアスの更新
    W -= alpha * dW
    b -= alpha * db
    
    # 100エポックごとに損失を表示
    if (epoch + 1) % 100 == 0:
        print(f'Epoch {epoch+1}/{epochs}, Loss: {loss:.4f}')

# モデルの最終的な損失を表示
print(f'最終損失: {loss:.4f}')

# テストデータでの予測関数
def predict(X, W, b):
    z = np.dot(X, W) + b
    y_pred = softmax(z)
    return np.argmax(y_pred, axis=1)

# テストデータで予測を行う
y_pred = predict(X, W, b)

# 結果を表示
print("予測ラベル: ", y_pred[:10])  # 最初の10サンプルの予測結果
print("実際のラベル: ", y[:10])  # 実際のラベル


import numpy as np
import matplotlib.pyplot as plt

# 位置符号化の関数を実装
def positional_encoding(position, d_model):
    PE = np.zeros((position, d_model))
    for pos in range(position):
        for i in range(0, d_model, 2):
            PE[pos, i] = np.sin(pos / (10000 ** (i / d_model)))
            if i + 1 < d_model:
                PE[pos, i + 1] = np.cos(pos / (10000 ** (i / d_model)))
    return PE

# パラメータ設定
position = 50  # トークンの位置
d_model = 16   # 埋め込みベクトルの次元

# 位置符号化の計算
PE = positional_encoding(position, d_model)

# 位置符号化を表示
plt.figure(figsize=(10, 6))
plt.pcolormesh(PE, cmap='viridis')
plt.xlabel("Embedding Dimensions")
plt.ylabel("Position")
plt.title("Positional Encoding")
plt.colorbar()
plt.show()


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?