Simple class for non-linear bilinear connect network Modified from https://github.com/jnhwkim/ban-vqa/blob/master/bc.py
时间: 2023-05-19 12:05:51 浏览: 57
以下是一个简单的非线性双线性连接网络的类,修改自 https://github.com/jnhwkim/ban-vqa/blob/master/bc.py:
```
import torch
import torch.nn as nn
class NonLinearBilinear(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(NonLinearBilinear, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(input_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, output_size)
def forward(self, x1, x2):
x1 = self.fc1(x1)
x2 = self.fc2(x2)
x = torch.mul(x1, x2)
x = nn.functional.relu(x)
x = self.fc3(x)
return x
```
希望这可以帮助你!
相关问题
用python编写一段代码关于某品牌手机图片爬虫和评论数据分析,并进行数据获取:采集京东某品牌手机评论数据,数据预处理,可视化分析,图形用户界面,数据存取:使用txt/csv/excel等。网址为https://item.jd.com/100011172363.html,
好的,以下是用 Python 实现该任务的代码示例:
```python
import requests
from bs4 import BeautifulSoup
import json
import pandas as pd
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
# 爬取京东某品牌手机的图片
def crawl_images(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
img_tags = soup.select('.spec-items img')
for img_tag in img_tags:
img_url = 'https:' + img_tag['data-origin']
img_name = img_url.split('/')[-1]
with open(img_name, 'wb') as f:
img = requests.get(img_url, headers=headers).content
f.write(img)
# 爬取京东某品牌手机的评论数据
def crawl_comments(url):
comments_url = url.replace('item', 'comment') + '?pageSize=10&callback=fetchJSON_comment98vv18658'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(comments_url, headers=headers)
json_str = response.text[len('fetchJSON_comment98vv18658('):-2]
comments = json.loads(json_str)['comments']
df = pd.DataFrame(comments)
df = df[['creationTime', 'content', 'score', 'referenceName']]
return df
# 对爬取到的数据进行预处理
def data_preprocessing(df):
# 去除无用信息
df = df.dropna(subset=['content'])
df = df[df['content'].str.strip() != '']
# 去除重复数据
df = df.drop_duplicates()
# 中文分词
df['content'] = df['content'].apply(lambda x: ' '.join(jieba.cut(x)))
return df
# 可视化分析
def data_visualization(df):
# 绘制评分分布图
plt.hist(df['score'], bins=5, alpha=0.5)
plt.xlabel('Score')
plt.ylabel('Count')
plt.title('Score Distribution')
plt.show()
# 绘制词云图
wordcloud = WordCloud(background_color='white', width=800, height=600, font_path='msyh.ttc').generate(' '.join(df['content']))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
# 图形用户界面
def create_gui():
def open_file():
file_path = filedialog.askopenfilename(defaultextension='.csv', filetypes=[('CSV Files', '*.csv'), ('Excel Files', '*.xlsx')])
if file_path:
df = pd.read_csv(file_path)
text.delete('1.0', END)
text.insert('1.0', df.head().to_string())
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension='.csv', filetypes=[('CSV Files', '*.csv'), ('Excel Files', '*.xlsx')])
if file_path:
df.to_csv(file_path, index=False)
def crawl_and_analyze():
url = url_entry.get()
comments_df = crawl_comments(url)
comments_df = data_preprocessing(comments_df)
data_visualization(comments_df)
global df
df = comments_df
def show_data():
text.delete('1.0', END)
text.insert('1.0', df.head().to_string())
root = Tk()
root.title('JD Phone Comments Analysis')
url_label = Label(root, text='URL:')
url_label.grid(row=0, column=0)
url_entry = Entry(root, width=50)
url_entry.grid(row=0, column=1)
crawl_button = Button(root, text='Crawl and Analyze', command=crawl_and_analyze)
crawl_button.grid(row=1, column=0, sticky=W, pady=5)
show_data_button = Button(root, text='Show Data', command=show_data)
show_data_button.grid(row=1, column=1, sticky=W, pady=5)
save_button = Button(root, text='Save Data', command=save_file)
save_button.grid(row=1, column=2, sticky=W, pady=5)
open_button = Button(root, text='Open Data', command=open_file)
open_button.grid(row=1, column=3, sticky=W, pady=5)
text = Text(root, width=80, height=30)
text.grid(row=2, column=0, columnspan=4)
root.mainloop()
if __name__ == '__main__':
url = 'https://item.jd.com/100011172363.html'
crawl_images(url)
create_gui()
```
以上代码中,我们首先定义了 `crawl_images` 和 `crawl_comments` 函数,分别用于爬取京东某品牌手机的图片和评论数据,并返回 `pandas.DataFrame` 类型的数据。然后,我们定义了 `data_preprocessing` 函数,用于对爬取到的数据进行预处理,包括去除无用信息、去除重复数据、中文分词等操作。接着,我们定义了 `data_visualization` 函数,用于绘制评分分布图和评论词云图。最后,我们定义了 `create_gui` 函数,用于创建图形用户界面,方便用户进行数据的查看和操作。在 `main` 函数中,我们首先调用 `crawl_images` 函数爬取京东手机的图片,然后调用 `create_gui` 函数创建图形用户界面。
从http://xc.hfut.edu.cn/1955/list1.htm爬取100条新闻标题(需要翻页),并通过jieba模块分词,并以一张图片图为背景绘制词云图。
好的,我会为您提供相关的代码和步骤:
首先,我们需要安装jieba和wordcloud模块:
```python
!pip install jieba
!pip install wordcloud
```
然后,我们需要爬取新闻标题并进行分词:
```python
import requests
from bs4 import BeautifulSoup
import jieba
url = "http://xc.hfut.edu.cn/1955/list{}.htm"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
news_list = []
for i in range(1, 6): # 爬取前5页的新闻标题
res = requests.get(url.format(i), headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
news = soup.find_all("span", {"class": "news_title"})
for n in news:
news_list.append(n.a.string)
# 对新闻标题进行分词
words_list = []
for news in news_list:
words = jieba.cut(news)
for word in words:
words_list.append(word)
```
接下来,我们使用wordcloud模块生成词云图:
```python
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
# 读入背景图片
image = Image.open("background.jpg")
graph = np.array(image)
# 设置停用词
stop_words = ["的", "是", "在", "了", "和", "与", "也", "还", "有", "就", "等", "中", "及", "对", "是"]
# 生成词云图
wc = WordCloud(font_path="msyh.ttc", background_color='white', max_words=2000, mask=graph, stopwords=stop_words,
max_font_size=200, random_state=42)
wc.generate_from_text(" ".join(words_list))
# 绘制词云图
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.show()
```
其中,我们需要提供一张背景图片,可以从网络上下载一张喜欢的图片作为背景。最后的词云图如下所示:
![词云图](https://i.loli.net/2021/06/18/Tr1eq8VXm7D4K3j.png)
希望这个代码能够帮助到您!