Compare commits

...

2 Commits

Author SHA1 Message Date
Lashay Yang 2315ea7278 update commit
6 years ago
Lashay Yang 95458cbb4d update commit
6 years ago

205
1.cpp

@ -1,205 +0,0 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{ //定义数据类型
int max, min;
} Data;
int MIN; //全部变量
//通过函数传回最大值,通过全局变量传回最小值
int fun1(int a[], int n){
int i, max;
max = MIN = a[0];
for(i=0; i<n; i++){
if(a[i]>max)
max = a[i];
if(a[i]<MIN)
MIN = a[i];
}
return(max);
}
//通过数组传回最大最小值
int *fun2(int a[], int n){
static int b[2];
b[0] = b[1] = a[0];
int i;
for(i=1; i<n; i++){
if(a[i] > b[0])
b[0] = a[i];
if(a[i] < b[1])
b[1] = a[i];
}
return(b);
}
//通过结构体指针传回最大最小值
Data *fun3(int a[], int n){
Data *p;
int i;
p = (Data *)malloc(sizeof(Data));
p->max = p->min = a[0];
for(i=1; i<n; i++){
if(a[i] > p->max)
p->max = a[i];
if(a[i] < p->min)
p->min = a[i];
}
return(p);
}
//通过结构体传回最大最小值
Data fun4(int a[], int n){
Data p;
int i;
p.max = p.min = a[0];
for(i=1; i<n; i++){
if(a[i] > p.max)
p.max = a[i];
if(a[i] < p.min)
p.min = a[i];
}
return (p);
}
//通过指针传回最大最小值
void fun5(int a[], int n, int *p, int *q){
int i ;
*p = *q = a[0];
for(i=1; i<n; i++){
if(*p < a[i])
*p = a[i];
if(*q > a[i])
*q = a[i];
}
}
main(){
int a[10] = {1,3,9,8,4,2,5,0,7,6}, max, *p;
Data *q;
Data z;
int x, y;
max = fun1(a,10);
printf("\n\n\t fun1: max == %d \t min == %d",max,MIN);
p = fun2(a,10);
printf("\n\n\t fun2: max == %d \t min == %d",p[0],p[1]);
q = fun3(a,10);
printf("\n\n\t fun3: max == %d \t min == %d",q->max,q->min);
z = fun4(a,10);
printf("\n\n\t fun4: max == %d \t min == %d",z.max,z.min);
fun5(a,10,&x,&y);
printf("\n\n\t fun5: max == %d \t min == %d",x,y);
}
////两个整数的值互换,局部有效
//void swap1(int a, int b){
// int c;
// c = a;
// a = b;
// b = c;
// printf("\n\n\t swap1: a = %d, b = %d",a,b);
//}
//
////两个整数的地址互换,全局有效
//void swap2(int *a, int *b){
// int c;
// c = *a;
// *a = *b;
// *b = c;
//}
//
//main(){
// int x = 100,y = 800;
// swap1(x,y);
// printf("\n\n\t swap1: x = %d, y = %d",x,y); //x,y的值不变
// x = 100, y = 800;
// swap2(&x,&y);
// printf("\n\n\t swap2: x = %d, y = %d",x,y); //x,y的值改变
//}
////定义复数的数据类型
//typedef struct {
// float realpart;
// float imagpart;
//}Complex;
//
////函数声明
//Complex create(float x, float y); //功能:构建复数
//Complex add(Complex z1, Complex z2); //功能:两个复数相加
//
////函数定义
//Complex create(float x, float y){
// Complex z;
// z.realpart = x;
// z.imagpart = y;
// return(z);
//}
//
//Complex add(Complex z1, Complex z2){
// Complex sum;
// sum.realpart = z1.realpart + z2.realpart;
// sum.imagpart = z1.imagpart + z2.imagpart;
// return(sum);
//}
//
////主函数
//main(){
// float a,b;
// Complex c1,c2,c3;
// printf("\n\n\n\t Input realpart and imagpart:"); //要求输入第一个复数的实部和虚部,用空格隔开
// scanf("%f %f",&a,&b);
// c1 = create(a,b); //构建第一个复数
// printf("\n\n\n\t Input realpart and imagpart:"); //要求输入第二个复数的实部和虚部,用空格隔开
// scanf("%f %f",&a,&b);
// c2 = create(a,b); //构建第二个复数
// c3 = add(c1,c2); //两个复数相加
// //输出结果
// printf("\n\n\n\t c1 == %f + %fi",c1.realpart,c1.imagpart);
// printf("\n\n\n\t c2 == %f + %fi",c2.realpart,c2.imagpart);
// printf("\n\n\n\t c3 == c1 + c2 == %f + %fi",c3.realpart,c3.imagpart);
//}
//int main(){
// int sum=0, avg=0, i=0;
// int t[10] = {80,85,77,56,68,83,90,92,80,98};
// for(i=0; i<10; i++)
// {
// sum += t[i];
// }
// avg = sum/10;
// printf("sum=%d\navg=%d\n",sum,avg);
// return 0;
//}
//int main(){
// int sum, avg;
// int t1=80, t2=85, t3=77, t4=56, t5=68, t6=83, t7=90, t8=92, t9=80, t10=98;
// sum = t1+t2+t3+t4+t5+t6+t7+t8+t9+t10;
// avg = sum/10;
// printf("sum=%d\navg=%d\n",sum,avg);
// return 0;
//}
//int main(){
// int n = 3;
// float x, max = 0;
// for(int i=1; i<=n; i++)
// {
// scanf("%f", &x);
// if(x>max)
// max = x;
// }
// printf("%f",max);
// return 0;
//}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,318 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\Program Files\\ANACOND3-5.2\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
" from ._conv import register_converters as _register_converters\n",
"Using TensorFlow backend.\n",
"D:\\Program Files\\ANACOND3-5.2\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:469: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
"D:\\Program Files\\ANACOND3-5.2\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:470: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
"D:\\Program Files\\ANACOND3-5.2\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:471: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
"D:\\Program Files\\ANACOND3-5.2\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:472: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
"D:\\Program Files\\ANACOND3-5.2\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:473: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
"D:\\Program Files\\ANACOND3-5.2\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:476: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
" np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"60000 train samples\n",
"10000 test samples\n"
]
}
],
"source": [
"'''Trains a simple deep NN on the MNIST dataset.\n",
"Gets to 98.40% test accuracy after 20 epochs\n",
"(there is *a lot* of margin for parameter tuning).\n",
"2 seconds per epoch on a K520 GPU.\n",
"'''\n",
"#import\n",
"from __future__ import print_function\n",
"\n",
"import keras\n",
"from keras.datasets import mnist\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Dropout\n",
"from keras.optimizers import RMSprop\n",
"from keras.utils import np_utils\n",
"\n",
"batch_size = 128\n",
"num_classes = 10\n",
"epochs = 20\n",
"\n",
"# the data, split between train and test sets\n",
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
"\n",
"x_train = x_train.reshape(60000, 784)\n",
"x_test = x_test.reshape(10000, 784)\n",
"x_train = x_train.astype('float32') #数据类型转换\n",
"x_test = x_test.astype('float32')\n",
"x_train /= 255 #数据归一化0,1\n",
"x_test /= 255\n",
"print(x_train.shape[0], 'train samples')\n",
"print(x_test.shape[0], 'test samples')\n",
"\n",
"#Using TensorFlow backend."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# convert class vectors to binary class matrices\n",
"y_train = keras.utils.to_categorical(y_train, num_classes)\n",
"y_test = keras.utils.to_categorical(y_test, num_classes)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"dense_1 (Dense) (None, 512) 401920 \n",
"_________________________________________________________________\n",
"dropout_1 (Dropout) (None, 512) 0 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 512) 262656 \n",
"_________________________________________________________________\n",
"dropout_2 (Dropout) (None, 512) 0 \n",
"_________________________________________________________________\n",
"dense_3 (Dense) (None, 10) 5130 \n",
"=================================================================\n",
"Total params: 669,706\n",
"Trainable params: 669,706\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model = Sequential()\n",
"model.add(Dense(512, activation='relu', input_shape=(784,)))\n",
"model.add(Dropout(0.2))\n",
"model.add(Dense(512, activation='relu'))\n",
"model.add(Dropout(0.2))\n",
"model.add(Dense(num_classes, activation='softmax'))\n",
"\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 60000 samples, validate on 10000 samples\n",
"Epoch 1/20\n",
"60000/60000 [==============================] - 9s - loss: 0.2453 - acc: 0.9235 - val_loss: 0.1407 - val_acc: 0.9563\n",
"Epoch 2/20\n",
"60000/60000 [==============================] - 9s - loss: 0.1014 - acc: 0.9694 - val_loss: 0.0901 - val_acc: 0.9737\n",
"Epoch 3/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0750 - acc: 0.9771 - val_loss: 0.0843 - val_acc: 0.9739\n",
"Epoch 4/20\n",
"60000/60000 [==============================] - 10s - loss: 0.0609 - acc: 0.9820 - val_loss: 0.0722 - val_acc: 0.9802\n",
"Epoch 5/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0497 - acc: 0.9853 - val_loss: 0.0842 - val_acc: 0.9781\n",
"Epoch 6/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0439 - acc: 0.9870 - val_loss: 0.0790 - val_acc: 0.9810\n",
"Epoch 7/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0390 - acc: 0.9884 - val_loss: 0.0759 - val_acc: 0.9827\n",
"Epoch 8/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0335 - acc: 0.9899 - val_loss: 0.0874 - val_acc: 0.9814\n",
"Epoch 9/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0331 - acc: 0.9905 - val_loss: 0.0794 - val_acc: 0.9823\n",
"Epoch 10/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0304 - acc: 0.9917 - val_loss: 0.0835 - val_acc: 0.9845\n",
"Epoch 11/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0263 - acc: 0.9923 - val_loss: 0.0929 - val_acc: 0.9829\n",
"Epoch 12/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0256 - acc: 0.9926 - val_loss: 0.1003 - val_acc: 0.9816\n",
"Epoch 13/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0252 - acc: 0.9932 - val_loss: 0.1114 - val_acc: 0.9826\n",
"Epoch 14/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0239 - acc: 0.9936 - val_loss: 0.1034 - val_acc: 0.9821acc: 0.9\n",
"Epoch 15/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0230 - acc: 0.9938 - val_loss: 0.1117 - val_acc: 0.98230.993 - ETA: 0s - loss: 0.0232 - acc: 0.99\n",
"Epoch 16/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0210 - acc: 0.9941 - val_loss: 0.0968 - val_acc: 0.9841\n",
"Epoch 17/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0185 - acc: 0.9951 - val_loss: 0.1044 - val_acc: 0.9829\n",
"Epoch 18/20\n",
"60000/60000 [==============================] - 9s - loss: 0.0200 - acc: 0.9947 - val_loss: 0.0968 - val_acc: 0.9842\n",
"Epoch 19/20\n",
"60000/60000 [==============================] - 10s - loss: 0.0182 - acc: 0.9956 - val_loss: 0.1225 - val_acc: 0.9816\n",
"Epoch 20/20\n",
"60000/60000 [==============================] - 10s - loss: 0.0195 - acc: 0.9953 - val_loss: 0.1110 - val_acc: 0.9818\n",
"Test loss: 0.11099309864256515\n",
"Test accuracy: 0.9818\n"
]
}
],
"source": [
"model.compile(loss='categorical_crossentropy',\n",
" optimizer=RMSprop(),\n",
" metrics=['accuracy'])\n",
"\n",
"history = model.fit(x_train, y_train,\n",
" batch_size=batch_size,\n",
" epochs=epochs,\n",
" verbose=1,\n",
" validation_data=(x_test, y_test))\n",
"score = model.evaluate(x_test, y_test, verbose=0)\n",
"print('Test loss:', score[0])\n",
"print('Test accuracy:', score[1])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saving model to disk \n",
"\n",
"Saved model to disk\n"
]
}
],
"source": [
"from keras.models import load_model\n",
"#Save the model\n",
"# # serialize model to JSON\n",
"# model_digit_json = model.to_json()\n",
"# with open(\"model_digit.json\", \"w\") as json_file:\n",
"# json_file.write(model_digit_json)\n",
"# # serialize weights to HDF5\n",
"# model.save_weights(\"model_digit.h5\")\n",
"# model.save('model_digit.h5') # creates a HDF5 file 'my_model.h5'\n",
"# save model\n",
"print(\"Saving model to disk \\n\")\n",
"mp = \"model_digit.h5\"\n",
"model.save(mp)\n",
"\n",
"print(\"Saved model to disk\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from keras.models import load_model\n",
"\n",
"\n",
"# load json and create model\n",
"\n",
"# json_file = open('model_digit.json', 'r')\n",
"\n",
"# loaded_model_json = json_file.read()\n",
"\n",
"# json_file.close()\n",
"\n",
"# loaded_model = model_from_json(loaded_model_json)\n",
"\n",
"# loaded_model.load_weights(\"model_digit.h5\")\n",
"\n",
"# print(\"Loaded model from disk\")\n",
"\n",
"model = load_model('model_digit.h5') #选取自己的.h模型名称\n",
"\n",
"image = cv2.imread('2.png')\n",
"img = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) # RGB图像转为gray\n",
"\n",
"img1 = x_test[500]\n",
"\n",
"#需要用reshape定义出例子的个数图片的 通道数图片的长与宽。具体的参看keras文档\n",
"img = (img1.reshape(1,784)).astype('float32')/255 \n",
"predict = model.predict_classes(img)\n",
"print ('识别为:')\n",
"print (predict)\n",
"\n",
"cv2.imshow(\"Image1\", image)\n",
"cv2.waitKey(0)\n",
"\n",
"# %matplotlib inline\n",
"# import matplotlib\n",
"# import matplotlib.pyplot as plt\n",
"\n",
"# def plot_digit(data):\n",
"# image = data.reshape(28, 28)\n",
"# plt.imshow(image, cmap = matplotlib.cm.binary,\n",
"# interpolation=\"nearest\")\n",
"# plt.axis(\"off\")\n",
" \n",
"# some_index = 5000\n",
"\n",
"# plt.subplot(121) #1代表行2代表列所以一共有2个图1代表此时绘制第二个图。\n",
"# plot_digit(x_test[some_index])\n",
"# # plt.subplot(121); plot_digit(y_test[some_index])\n",
"# # save_fig(\"noisy_digit_example_plot\")\n",
"# plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading…
Cancel
Save