0
点赞
收藏
分享

微信扫一扫

tensorflow学习笔记(一)

辰鑫chenxin 2022-05-01 阅读 30

tensorflow 的含义

tensor的英文含义为张量,可以理解为一个任意维的矩阵,一个可以进行GPU计算的矩阵,tensorflow表示为对矩阵进行计算。

简单操作

import tensorflow as tf
import numpy as np

#检查tensorflow版本 
tf.__version__ 
2.8.0

x=[[1.]]
m=tf.matmul(x,x)#将矩阵 a 乘以矩阵 b,生成a * b
print(m)

tf.Tensor([[1.]], shape=(1, 1), dtype=float32)

x=tf.constant([[1,9],[3,6]])
tf.Tensor(
[[1 9]
 [3 6]], shape=(2, 2), dtype=int32)

x=tf.add(x,1)
tf.Tensor(
[[ 2 10]
 [ 4  7]], shape=(2, 2), dtype=int32)

转换格式

x.numpy()
[[ 2 10]
 [ 4  7]]

x=tf.cast(x,tf.float32)#格式转换为浮点
tf.Tensor(
[[ 2. 10.]
 [ 4.  7.]], shape=(2, 2), dtype=float32)

回归问题预测

在tensorflow2中将大量使用keras的简介建模方法

import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.keras import layers
import tensorflow.python.keras
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

使用的是一个温度的数据集来进行实验

features = pd.read_csv('temps0.csv')
print(features.head())

   year  month  day  week  temp_2  temp_1  average  friend
0  2016      1    1   Fri      45      45     45.6      29
1  2016      1    2   Sat      44      45     45.7      61
2  2016      1    3   Sun      45      44     45.8      56
3  2016      1    4   Mon      44      41     45.9      53
4  2016      1    5  Tues      41      40     46.0      41


print("数据维度",features.shape)
数据维度 (348, 8)

处理时间数据

import datetime
years=features['year']
months=features['month']
days=features['day']
#datatime格式
dates=[str(int(year))+'-'+str(int(month))+'-'+str(int(day)) for year ,month ,day in zip(years,months,days)]
dates=[datetime.datetime.strptime(date,'%Y-%m-%d')for date in dates]
print(dates[:5])

[datetime.datetime(2016, 1, 1, 0, 0), datetime.datetime(2016, 1, 2, 0, 0), datetime.datetime(2016, 1, 3, 0, 0), datetime.datetime(2016, 1, 4, 0, 0), datetime.datetime(2016, 1, 5, 0, 0)]

准备画图,再次之间要进行数据处理,删除异常数据,清洗数据等。

#将所有信息转为数字信息
features=pd.get_dummies(features)
print(features.head(5))

   year  month  day  temp_2  ...  week_Sun  week_Thurs  week_Tues  week_Wed
0  2016      1    1      45  ...         0           0          0         0
1  2016      1    2      44  ...         0           0          0         0
2  2016      1    3      45  ...         1           0          0         0
3  2016      1    4      44  ...         0           0          0         0
4  2016      1    5      41  ...         0           0          1         0

[5 rows x 14 columns]

#标签值
#去除特征中的标签
features=features.drop('actual',axis=1)
#名字单独保存一下,
features_list=list(features.columns)
#转换成合适的格式
features=np.array(features)
print(features.shape)

(348, 14)

进行归一化操作

from sklearn import preprocessing
input_features=preprocessing.StandardScaler().fit_transform(features)
print(input_features[0])

基于Keras构建网络模型

一些常用的参数
在这里插入图片描述

model=tf.keras.Sequential()
model.add(layers.Dense(16))#全连接层
model.add(layers.Dense(32))
model.add(layers.Dense(1))

compile相当于对网络进行配置,指定好优化器和损失函数等

model.compile(optimizer=tf.keras.optimizers.SGD(0.001),loss='mean_squared_error')
model.fit(input_features,labels,validation_split=0.25,epochs=10,batch_size=64)
举报

相关推荐

0 条评论